Archive

Posts Tagged ‘WCF’

WCF Instance De-Activation

September 29, 2010 Leave a comment

So we are going to take a look at WCF Instance de-activation.

When you have the InstanceContextMode to Per Call the service instance is deactivated at the end of the call.
When you have the InstanceContextMode to Per Session the service instance is deactivated at the end of the session or after timeout.
When you have the InstanceContextMode to Single the service instance is deactivated at the shutdown of the application.

You need to apply the ReleaseInstanceMode as shown below. The options are pretty self explanatory as to when to release the service instance.

You can also do it in code using OperationContext.Current.InstanceContext.ReleaseServiceInstance() as shown in the code.

using System;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace WCFInstanceActivation.Server
{
    [ServiceContract(Namespace = "http://localhost/Samples", SessionMode = SessionMode.Required)]
    interface IPrintService
    {
        [OperationContract]
        void Print(string message);
    }

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    class PrintService : IPrintService
    {

        [OperationBehavior(ReleaseInstanceMode = ReleaseInstanceMode.BeforeCall)]
        public void Print(string message)
        {
            Console.WriteLine(message);
            OperationContext.Current.InstanceContext.ReleaseServiceInstance();

            Console.WriteLine("Continuing with session id: " + OperationContext.Current.SessionId);
        }
    }

    class Program
    {
        static void Main()
        {
            using (ServiceHost host = new ServiceHost(typeof(PrintService), new Uri("http://localhost/PrintService")))
            {

                host.AddServiceEndpoint(typeof(IPrintService), new WSHttpBinding(), "");
                host.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true });
                //host.Description.Behaviors.Add(new ServiceDebugBehavior{ IncludeExceptionDetailInFaults = false});

                host.Open();

                Console.WriteLine("Press any key to quit..");
                Console.ReadKey();
            }
        }
    }
}

Hope this helps..

Blair..

Categories: Development Tags: , , , , , ,

WCF DataContracts Exposing Subclasses via WSDL

September 27, 2010 Leave a comment

To expose your subclasses via WSDL through metadata they next to be marked as a DataContract and added to the parent classes KnownType attribute.

using System;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace WCFDataContracts.Server
{

    [DataContract]
    [KnownType(typeof(GoldCustomer))]
    public class Customer
    {

        [DataMember]
        public string Name { get; set; }
    }

    [DataContract]
    public class GoldCustomer : Customer
    {
    }

    [ServiceContract]
    public interface IPrintService
    {

        [OperationContract]
        void Print(Customer c);
    }

    public class PrintService : IPrintService
    {
        public void Print(Customer c)
        {
            Console.Write(c.Name);
        }
    }

    [ServiceContract]
    public interface ICustomerService
    {

        [OperationContract]
        void Print(Customer c);
    }

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    class CustomerService : ICustomerService
    {
        public void Print(Customer c)
        {
            Console.WriteLine(c.Name);
        }
    }

    class Program
    {
        static void Main()
        {
            Console.WriteLine("Starting Service Host");

            using(ServiceHost host = new ServiceHost(typeof(PrintService),new Uri("http://localhost/PrintService")))
            {
                ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
                behavior.HttpGetEnabled = true;
                host.Description.Behaviors.Add(behavior);

                host.AddServiceEndpoint(typeof(IPrintService), new WSHttpBinding(), "");

                host.Open();

                Console.WriteLine("Press any key to exit..");
                Console.ReadKey();

            }

        }
    }
}

Hope this helps.
Blair…

Adding Metadata manually and using custom addresses

March 18, 2010 Leave a comment

Okay this is how you add a service in code with endpoints on different addresses.

Uri baseAddress = new Uri("http://localhost:8088/Printer/");

ServiceHost host = new ServiceHost(typeof(Printer),baseAddress);
host.AddServiceEndpoint(typeof (IPrinter), new WSHttpBinding(SecurityMode.None), "");
host.Description.Behaviors.Add(new ServiceMetadataBehavior()
                                               {
                                                   HttpGetEnabled = true,
                                                   HttpGetUrl = new Uri("http://localhost:8088/Elite")
                                               });
host.AddServiceEndpoint(
     ServiceMetadataBehavior.MexContractName,MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
host.Open();
Console.ReadKey();
 host.Close();

Blair…

Categories: Development Tags: , , , ,

Understanding REST

March 17, 2010 Leave a comment
Categories: Development Tags: , ,

Basic GET REST based support in WCF

March 15, 2010 Leave a comment

I have recently just added support for REST for a client I am doing some work for at the moment. Their was some okay documentation on the net, but I found a lot of it quite fragmented. This is not an introduction into what REST is but just what you need to get it working in a read only manner in WCF. I will also not be covering security or anything along those lines along with WCF basics.

So the first thing you need to get this working is ensuring that you have enabled data contract support on your DTOs. Then you need to add ASPNETCompatability to your service host. You need the following in you app config file inside the services section.

<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>

You also need to tell your class it can use this mode but decorating the CONCRETE class with the following snippet
[AspNetCompatibilityRequirements(RequirementsMode =aspNetCompatibilityRequirementsMode.Allowed)]

You also need a webHttpBinding endpoint as shown here

<endpoint address="RestTestService" binding="webHttpBinding"     contract="Test.WebServices.Services.ITestService" behaviorConfiguration="testWebEndpointBehaviour" >
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>

Create a endpoint behavior to enable web http as shown here:

 <endpointBehaviors>
        <behavior name="testWebEndpointBehaviour">
          <webHttp/>
        </behavior>
      </endpointBehaviors>

Lastly on the service interfaces add the WebGet attributes with the URITemplate you want.

[OperationContract]
[WebGet(UriTemplate = "Projects",ResponseFormat = WebMessageFormat.Json)]

Finally use a browser to test.

Blair….

Categories: Development Tags: , , , , ,

WCF Throttling and Quotas

March 14, 2010 Leave a comment

Came across a great post by Dan Rigsby which had the exact information I was looking for trying to understand reader quotas in WCF. There is heaps of info on the net that I thought was not that helpful. This post located here was really good. Also his site has a lot of great information.

Blair..

Categories: Development Tags: , , ,

WCF Instance Context

March 11, 2010 1 comment

The Instance Context in WCF represents the running instance of a service along with its state. Im reading a WCF book at the moment and it mentions that the context holds the service instance. After some playing around and creating a test solution i created two services 1 that was a session and a per call. If you put the the following code in a method marked with operationcontract and break point on it you get some interesting results.

var hcode = OperationContext.Current.InstanceContext.GetServiceInstance().GetHashCode();

If it is a session method the hash code will be the same every-time and per call will be different. Basically I think this object holds the service instance and acts as a broker for instancing. It also provides some useful context information on the service instance environment.

See Dan Rigsby’s Great Post here.

Blair…

Categories: Development Tags: , ,