Solved

Using Properties with a WCF Service

Posted on 2014-11-27
7
224 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

705 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now