Solved

Using Properties with a WCF Service

Posted on 2014-11-27
7
260 Views
Last Modified: 2014-11-27
Having never touched WCF before, I am having a play now.

The first thing I wanted to do, was restrict people from using certain methods until after they had successfully authenticated.

For testing purposes, I added a class level boolean property which I set to true inside an Authenticate method. My theory was that I could then simple check that variable in each method they called to see if I needed to throw them out, or allow them to continue on.

What I found though, was that, even though one method would set the property to True, the next method would read it's value as False again.

This led me to wonder if you can even use properties in WCF services, or do they go out of scope after each method call?

I found a couple of articles, which thankfully assured me that I wasn't the first person to wonder this, but even following their code tweaks, I couldn't get a class level variable to 'stick'.

http://tech.pro/tutorial/908/wcf-tip-using-properties-in-service-contracts

http://visualstudiomagazine.com/blogs/tool-tracker/2012/11/using-properties-in-wcf-services.aspx

Am I going about this wrong way?
0
Comment
Question by:JedNebula
  • 4
  • 3
7 Comments
 
LVL 11

Accepted Solution

by:
melmers earned 500 total points
ID: 40468730
Hello,

is your WCF service in session mode ?
http://msdn.microsoft.com/en-us/library/ms733040%28v=vs.110%29.aspx

Because if not it could be that every call creates a new instance
0
 
LVL 1

Author Comment

by:JedNebula
ID: 40468853
Hi Melmers,

Thank you for your reply.

I have changed my Interface Decoration to:
<ServiceContract(SessionMode:=SessionMode.Required)> _

Open in new window


but unfortunatey, I now receive the following error when trying to update the Service Reference in my client app:
 
Contract requires Session, but Binding 'BasicHttpBinding' doesn't support it or isn't configured properly to support it

I'm not too sure how to change the Binding to not use BasicHttp to be honest. Is it done in the client App.Config, or in the Server project somewhere? The server project only has a web.config as I am also running a Web API project alongside. Here is the content:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=301879
  -->
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <!-- <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=&quot;|DataDirectory|\aspnet-Web API-20141124034010.mdf&quot;;Initial Catalog=&quot;aspnet-Web API-20141124034010&quot;;Integrated Security=True" providerName="System.Data.SqlClient" />-->
    <add name="DefaultConnection" connectionString="Data Source=rawden8;Initial Catalog=EskimoLicensing;Integrated Security=True" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <appSettings></appSettings>
  <system.web>
    <authentication mode="None" />
    <compilation debug="true" targetFramework="4.5.1" />
    <httpRuntime targetFramework="4.5.1" />
  </system.web>
  <system.webServer>
    <modules>
      <remove name="FormsAuthentication" />
    </modules>
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.2.0" newVersion="5.2.2.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"   />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
      multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
</configuration>

Open in new window

0
 
LVL 11

Expert Comment

by:melmers
ID: 40468865
Add this code to your web.config
<!-- This is existing in your code -->
<system.serviceModel>
<!-- Add this from here -->
    <services>
<!-- change the name and contract to your needs -->
      <service name="Service">
        <endpoint binding="wsHttpBinding" contract="IService">endpoint>

      service>
    <services>
<!-- End add -->

Open in new window

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 1

Author Comment

by:JedNebula
ID: 40468884
Ok thanks.

It compiles okay, but it has the following 2 warnings.

I also can't get the service to add to the client add. It's still trying to use Basic HTTP for some reason.
27-11-2014-12-57-07-PM.jpg
27-11-2014-01-01-02-PM.jpg
0
 
LVL 11

Expert Comment

by:melmers
ID: 40468894
You can try to add this to the ServiceModel tag.
 <protocolMapping>
  <add scheme="http" binding="wsHttpBinding"/>
</protocolMapping>

Open in new window


and change the following
<endpoint binding="wsHttpBinding" contract="IService">endpoint>

to

<endpoint binding="wsHttpBinding" contract="IEskimoAPISevice">endpoint>

Open in new window

0
 
LVL 1

Author Comment

by:JedNebula
ID: 40469036
No I'm afraid that made no difference.

Would someone be able to put together a sample app, which they can zip and post?

Hopefully I was clear enough in the original question as to what I am trying to achieve.

If this is possible at all, I'll be able to compare the project with mine and hopefully see where I am going wrong.
0
 
LVL 1

Author Closing Comment

by:JedNebula
ID: 40469154
In the end, I managed to get it working by downloading the code found here:

http://www.codeproject.com/Articles/127395/Implementing-a-WCF-Service-with-Entity-Framework

I copied the <system.serviceModel> section from his app.config into my Web.config and changed the relevant names.

All okay now - working with Sessions :-)

I could even take off the
SessionMode:=SessionMode.Required
attribute and it still worked.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.NET 2008 Publish Error 2 34
Help with adding DLL file in Windows project 20 40
SSIS Feed MS SQL Server Stored procedure with loop 4 37
SSIS Start 2 21
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question