Using Properties with a WCF Service

Posted on 2014-11-27
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 500 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 -->
    <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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now


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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
.NET Enums [Flags] & Bitwise Design Question 6 53
ASP.NET Built-In Report Creator / Viewer 5 41
Need to find a web event on the IIS server 4 52
Error in Visual Project 10 48
While working on Silverlight and WCF application, I faced one issue where fault exception occurred at WCF operation contract is not getting propagated to Silverlight client. So after searching net I came to know that it was behavior by default for s…
Here I am going to explain creating proxies at runtime for WCF Service. So basically we use to generate proxies using Add Service Reference and then giving the Url of the WCF service then generate proxy files at client side. Ok, what if something ge…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below.…

732 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