Creating a wcf proxy against ChannelFactory

Which of these two ways to use the wcf service is better? why?

  • Creating a proxy server from the help service
  • using ChannelFactory

ex.

ChannelFactory<IMyContract> factory = new ChannelFactory<IMyContract>();
IMyContract proxy1 = factory.CreateChannel();
proxy1.MyMethod();

It's a little boring to call a wcf service like so

IMyContract proxy1 = null; 
try
{
    proxy1 = factory.CreateChannel();
    proxy1.MyMethod();
    ((ICommunicationObject)proxy1).Close();
}
catch
{
   ((ICommunicationObject)proxy1).Abort();
}

Should I repeat this fragment for each proxy call? Or is there a general way to create a wrapper class to close and break proxies?

Is such a class ServiceExecution.Execute(proxy=>proxy.MyMethod());that creates a proxy and closes or aborts its good way to do this?

+5
source share
4 answers

- MSDN, .Net 3, ChanelFactory,.Net 3.5 ChanelFactory .

ChanelFactory, partials

+3

, VS Service Reference, , ServiceContrcats DataContracts.

ChannelFactory, .. .

+2

1.

blog , , , (, , ..). MSDN.

+2

- . svcutil - , Visual Studio . , - , , , -.

, - , TestService, 8000, Visual Studio : - TestServiceProxy.cs .

cd "C:\src\proxies"
svcutil /noLogo /out:TestServiceProxy http://localhost:8000/TestService

, :

/n:*,WcfServices.TestService -.

Add /config:TestServiceProxy.config, and svcutil will create a sample configuration file for using TestService, including endpoints, bindings, etc.

Add /r:"Common.dll", and the proxy class released by svcutil will not determine the types used by the service, but defined in the Common.dll assembly.

Use svcutil /?for more information.

0
source

All Articles