Using Properties with a WCF Service

Posted on 2014-11-27
Medium Priority
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'.



Am I going about this wrong way?
Question by:JedNebula
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 11

Accepted Solution

melmers earned 1500 total points
ID: 40468730

is your WCF service in session mode ?

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

Author Comment

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
    <!-- 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=, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <!-- <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" />
    <authentication mode="None" />
    <compilation debug="true" targetFramework="4.5.1" />
    <httpRuntime targetFramework="4.5.1" />
      <remove name="FormsAuthentication" />
      <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" />
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="" newVersion="" />
        <assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="" newVersion="" />
        <assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="" newVersion="" />
        <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="" newVersion="" />
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="" newVersion="" />
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="" newVersion="" />
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="" newVersion="" />
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="" newVersion="" />
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="" newVersion="" />
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="" newVersion="1.6.5135.21930" />
        <assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" culture="neutral" />
        <bindingRedirect oldVersion="" newVersion="" />
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
        <parameter value="mssqllocaldb" />
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"   />
          <serviceDebug includeExceptionDetailInFaults="true" />
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
      multipleSiteBindingsEnabled="true" />

Open in new window

LVL 11

Expert Comment

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

<!-- End add -->

Open in new window

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?


Author Comment

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.
LVL 11

Expert Comment

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

Open in new window

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


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

Open in new window


Author Comment

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.

Author Closing Comment

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


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
attribute and it still worked.

Featured Post

Certified OpenStack Administrator Course

We just refreshed our COA course based on the Newton exam.  With 14 labs, this course goes over the different OpenStack services that are part of the certification: Dashboard, Identity Service, Image Service, Networking, Compute, Object Storage, Block Storage, and Orchestration.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

752 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