2010-05-31 24 views

Respuesta

101

Tendrás que usar la clase ChannelFactory.

He aquí un ejemplo:

var myBinding = new BasicHttpBinding(); 
var myEndpoint = new EndpointAddress("http://localhost/myservice"); 
var myChannelFactory = new ChannelFactory<IMyService>(myBinding, myEndpoint); 

IMyService client = null; 

try 
{ 
    client = myChannelFactory.CreateChannel(); 
    client.MyServiceOperation(); 
    ((ICommunicationObject)client).Close(); 
} 
catch 
{ 
    if (client != null) 
    { 
     ((ICommunicationObject)client).Abort(); 
    } 
} 

recursos relacionados:

+3

Genial, gracias. Como complemento, a continuación se explica cómo utilizar el objeto IMyService en su aplicación: http://msdn.microsoft.com/en-us/library/ms733133.aspx – Andrei

+0

Debe convertir 'client' en' IClientClient' en para cerrarlo sin embargo. – Dyppl

+0

En mi ejemplo, asumo que la interfaz 'IMyService' hereda de [System.ServiceModel.ICommunicationObject] (http://msdn.microsoft.com/en-us/library/system.servicemodel.icommunicationobject.aspx). Modifiqué el código de muestra para aclarar esto. –

6

También puede hacer lo que generó la "referencia de servicio" código hace

public class ServiceXClient : ClientBase<IServiceX>, IServiceX 
{ 
    public ServiceXClient() { } 

    public ServiceXClient(string endpointConfigurationName) : 
     base(endpointConfigurationName) { } 

    public ServiceXClient(string endpointConfigurationName, string remoteAddress) : 
     base(endpointConfigurationName, remoteAddress) { } 

    public ServiceXClient(string endpointConfigurationName, EndpointAddress remoteAddress) : 
     base(endpointConfigurationName, remoteAddress) { } 

    public ServiceXClient(Binding binding, EndpointAddress remoteAddress) : 
     base(binding, remoteAddress) { } 

    public bool ServiceXWork(string data, string otherParam) 
    { 
     return base.Channel.ServiceXWork(data, otherParam); 
    } 
} 

Dónde IServiceX es su contrato de servicio WCF

Luego, su código de cliente:

var client = new ServiceXClient(new WSHttpBinding(SecurityMode.None), new EndpointAddress("http://localhost:911")); 
client.ServiceXWork("data param", "otherParam param"); 
Cuestiones relacionadas