Janka's rambilng on boring stuff
Janka's Blog
8Jul/100

Disposing Proxy calls to WCF Services

Posted by Janak Gheewala

How many times have we had this issue, you call a WCF (of infact any other service) and due to some exception the the call to the service is not disposed and will eventually eat up all the resource.

A classic example of such issue is

<pre>
<pre>	public void Save(Message message)
	{
		IService _service = new ServiceClient();
		SaveMessageRequest request = new SaveMessageRequest{Message = message};
		_service.SaveMessage(request);
	}


To resolve this ussye we will make changes in two steps.

Step 1:

We will define a partial class with dispose method.


    public partial class ServiceClient : IDisposable
    {
        /// <summary>
        /// A partial class for ServiceClient
        /// Used to correctly close or abort a WCF proxy call.
        /// You MUST wrap the proxy call with a USING statement as DISPOSE will ALWAYS be called
        /// </summary>
        void IDisposable.Dispose()
        {
            // Check to see if the call has faulted
            if (State == CommunicationState.Faulted)
            {
                // We must abort the proxy as closing it will cause an exception
                Abort();
            }
            else
            {
                // Close the proxy
                Close();
            }
        }
    }

Step 2:

 We will change the save method to look as below. This will make sure that before the execution exits the using, the dispose method is called to dispose the service client.  

	public void Save(Message message)
	{
		using (ServiceClient smsProxy = new ServiceClient())
		{
			SaveMessageRequest request = new SaveMessageRequest{Message = message};
			smsProxy.SaveMessage(request);
		}
	}
  • Print
  • PDF
  • RSS

Share
11May/100

Adding a reference of a WCF task hosted into a test Harness

Posted by Janak Gheewala

TDD is the in thing...

Recently I had to write a WCF task which was to be hosted in Windows services rather than IIS.

Although I did write a some test cases around it, the business case I was working on required me to write a test harness (it was a long running process & hence the decision to host it in Windows service & write a test harness)

Now since I already had a WCF service host client within the solution (see this post for more details), I could not add the reference to the test harness without an error as shown below

System.ServiceModel.AddressAlreadyInUseException: There is already a listener on IP endpoint 0.0.0.0:8532. Make sure that you are not trying to use this endpoint multiple times in your application and that there are no other applications listening on this endpoint. ---> System.Net.Sockets.SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted

at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)

at System.Net.Sockets.Socket.Bind(EndPoint localEP)

at System.ServiceModel.Channels.SocketConnectionListener.Listen()

--- End of inner exception stack trace ---

at System.ServiceModel.Channels.SocketConnectionListener.Listen()

at System.ServiceModel.Channels.BufferedConnectionListener.Listen()

at System.ServiceModel.Channels.ExclusiveTcpTransportManager.OnOpen()

at System.ServiceModel.Channels.TransportManager.Open(TransportChannelListener channelListener)

at System.ServiceModel.Channels.TransportManagerContainer.Open(SelectTransportManagersCallback selectTransportManagerCallback)

at System.ServiceModel.Channels.ConnectionOrientedTransportChannelListener.OnOpen(TimeSpan timeout)

at System.ServiceModel.Channels.TcpChannelListener`2.OnOpen(TimeSpan timeout)

at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)

at System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen(TimeSpan timeout)

at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)

at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout)

at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)

at Microsoft.Tools.SvcHost.ServiceHostHelper.OpenService(ServiceInfo info)Only one usage of each socket address (protocol/network address/port) is normally permitted

The reason for this error is that as soon as your try and add a reference to the WCF task, it will spin up the existing WCF host client in the solution and hence the error above. To be fair, it will still add the reference to the test harness.

I wanted to get rid of this, it is then that I had a chat with Mr. Fenton and he suggested me a quick work around for this issue. Here's what I did.

* Remove the test harness from the solution
* spin up the test harness project as a separate project.
* then add the reference to the wcf task.

lo, job done.

  • Print
  • PDF
  • RSS

Share
Tagged as: , , No Comments
11May/100

Issues with hosting WCF in a Windows Service Using TCP

Posted by Janak Gheewala

I wanted to set up a WCF task hosted in windows services. I surfed a bit on the internet and found this explanatory example from microsoft. Job done? nay ... I could not start up service, When i checked the event log it had the following exception

Service cannot be started. System.InvalidOperationException: Service
'WindowsService1.Service1' has zero application (non-infrastructure)
endpoints. This might be because no configuration file was found for your
application, or because no service element matching the service name could
be found in the configuration file, or because no endpoints were defined in
the service element.
at
System.ServiceModel.Description.DispatcherBuilder.EnsureThereAreNonMexEndpoints(ServiceDescription
description)
at
System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription
description, ServiceHostBase serviceHost)
at System.ServiceModel.ServiceHostBase.InitializeRuntime()
at System.ServiceModel.ServiceHostBase.OnBeginOpen()
at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan
timeout)
at System.ServiceModel.Channels.CommunicationObject.Open()
at WindowsService1.Service1.OnStart(String[] args) in C:\Program
Files\...

It was after a bit of thinking I realised that there was a reference problem. Because the reference should be

myServiceHost = new ServiceHost(typeof(WcfServiceLibrary1.Service1));

and not
myServiceHost = new ServiceHost(typeof(Service1));

The reason for this is both the WcfService and Windows Service contains class as Service1 and if you use the Service1 as given in the link, it refers to WindowsService class file.

  • Print
  • PDF
  • RSS

Share
Tagged as: , No Comments
9May/100

Adding a reference of a WCF task hosted into a test Harness – Invalid Class Defination

Posted by Janak Gheewala

One of the difference that I found between hosting a WCF in IIS against hosting it in Windows Services is that you can't start the service if its not interfaced properly.

Let me give you an example


[ServiceBehavior(Namespace  = "some Namespace", Name = "some name")]

public class  ServiceClass: BaseClass

{

// some code

}

The above example failed in my case because the BaseClass was implementing and interface IBaseClass. To make the above example work I had to implement the interface as well.


[ServiceBehavior(Namespace  = "some Namespace", Name = "some name")]

public class  ServiceClass: BaseClass, IBaseClass

{

// some code

}
  • Print
  • PDF
  • RSS

Share
Tagged as: , , No Comments