How to create a generic WCF service proxy ?
2013-10-10 05:31
357 查看
/*
Author: Jiangong SUN
*/
WCF Service Client:
reference:
http://huseyincakir.wordpress.com/2009/03/08/wcf-services-in-net-without-using-%E2%80%9Cadd-service-reference/
Author: Jiangong SUN
*/
WCF Service Client:
/// <summary> /// A WCF service client. /// </summary> /// <typeparam name="TContract">The type of the service contract.</typeparam> internal class ServiceClient<TContract> : IDisposable where TContract : class { /// <summary> /// The service client object. /// </summary> private TContract client; /// <summary> /// Initializes a new instance of the ServiceClient class. /// </summary> /// <param name="endpointName">The name of the endpoint to use.</param> public ServiceClient(string endpointName) { ChannelFactory<TContract> f = new ChannelFactory<TContract>(endpointName); this.client = f.CreateChannel(); } /// <summary> /// Calls an operation on the service. /// </summary> /// <typeparam name="TResult">The type of the object returned by the service operation.</typeparam> /// <param name="operation">The operation to call.</param> /// <returns>The results of the service operation.</returns> public TResult ServiceOperation<TResult>(Func<TContract, TResult> operation) { return operation(this.client); } /// <summary> /// Calls an operation on the service. /// </summary> /// <param name="operation">The operation to call.</param> public void ServiceOperation(Action<TContract> operation) { operation(this.client); } /// <summary> /// Disposes this object. /// </summary> public void Dispose() { try { if (this.client != null) { IClientChannel channel = this.client as IClientChannel; if (channel.State == CommunicationState.Faulted) { channel.Abort(); } else { channel.Close(); } } } finally { this.client = null; GC.SuppressFinalize(this); } } }
[ServiceContract] public interface IHelloWorldService { [OperationContract] string GetMessage(string name); [OperationContract] string GetPersonCompleteInfo(Person person); } [DataContract] public class Person { [DataMember] public int Age { get; set; } [DataMember] public string Sex { get; set; } [DataMember] public string Name { get; set; } } static void Main(string[] args) { WSHttpBinding basicHttpBinding = new WSHttpBinding(); EndpointAddress endpointAddress = new EndpointAddress("http://localhost:8080/HostDevServer/HelloWorldService.svc"); var service = new ChannelFactory<IHelloWorldService>(basicHttpBinding, endpointAddress).CreateChannel(); var message = service.GetMessage("hello world!"); Console.WriteLine(message); }
reference:
http://huseyincakir.wordpress.com/2009/03/08/wcf-services-in-net-without-using-%E2%80%9Cadd-service-reference/
相关文章推荐
- How to create simple web service in VS2010, NOT WCF service
- How to create RESTful WCF Service
- How to create, host, test and consume a WCF Web Service
- What if you encounter a problem when consume your WCF service ? How to Diagnostic it ?
- How to create service which restarts on crash
- How to create WCF
- How to use the WCF Service Configuration Editor
- How to create (and deploy) a windows service in C# ?
- [Windows Azure] How to Create and Deploy a Cloud Service?
- How To Create GenericPrincipal Objects with Forms Authentication
- MSMQ&Com+ Service: How to create an Com+ Service in .NetFramework
- BW--如何创建一个BW的数据源(How to Create a Generic Datasource)
- BW--如何创建一个BW的数据源(How to Create a Generic Datasource)
- WCF分布式开发常见错误(3):客户端调用服务出错:You have tried to create a channel to a service that does not support .Net Framing
- BW--如何创建一个BW的数据源(How to Create a Generic Datasource)
- How to Pass IList As parameter to wcf service
- How To Create GenericPrincipal Objects with Forms Authentication(转MSDN)
- BW--如何创建一个BW的数据源(How to Create a Generic Datasource)
- How to Create a Windows NT/ Windows 2000 Service
- How to: Publish Metadata for a WCF Service.(What is the Metadata Exchange Endpoint purpose.)