如何以编程方式创建BasicHttpBinding?

时间:2021-05-06 23:06:43

I have to following code:

我必须遵循以下代码:

BasicHttpBinding binding = new BasicHttpBinding ();

Uri baseAddress = new Uri ("URL.svc");

EndpointAddress endpointAddress = new EndpointAddress (baseAddress);

var myChannelFactory = new ChannelFactory<IMyInterface> (binding, endpointAddress);

IMyInterface client = null;

try
{
    client = myChannelFactory.CreateChannel ();
    var a = client.WsFunction ("XXXXXX");                    
    ((ICommunicationObject)client).Close ();
}
catch
{
    if (client != null)
    {
        ((ICommunicationObject)client).Abort ();
    }
}

Where "IMyInterface" is the interface that my WS implements.. for example:

“IMyInterface”是我的WS实现的接口...例如:

[ServiceContract]
public interface IMyInterface
{
    [OperationContract]
    Result WsFunction1 (string param);

    [OperationContract]
    Result WsFunction2 (string param);

    [OperationContract]
    Result WsFunction3 (string param);
}

And it returns something like this:

它返回如下内容:

[DataContract]
public class Result
{
    string a = "";
    string b = "";

    [DataMember]
    public string A
    {
        get { return a; }
        set { a = value; }
    }

    [DataMember]
    public string B
    {
        get { return b; }
        set { b = value; }
    }
}

When I run this code, I can reach the WS, but I can never get the Result filled out.

当我运行此代码时,我可以访问WS,但我永远无法填写结果。

What am I doing wrong?

我究竟做错了什么?

Thanks in advance!

提前致谢!

3 个解决方案

#1


8  

The easiest way to access a service via a BasicHttpBinding is to generate the client code from SlSvcUtil.exe, which is a silverlight utility application.

通过BasicHttpBinding访问服务的最简单方法是从SlSvcUtil.exe生成客户端代码,这是一个silverlight实用程序。

SLsvcUtil.exe /directory:C:\users\me\Desktop http://URL.svc

That should create a MyInterfaceClient class inside of the file it generates.

这应该在它生成的文件中创建一个MyInterfaceClient类。

Then in your code you can do:

然后在您的代码中,您可以:

var binding = new BasicHttpBinding() {
    Name = "BindingName",
    MaxBufferSize = 2147483647,
    MaxReceivedMessageSize = 2147483647
};

var endpoint = new EndpointAddress("URL.svc");

MyInterfaceClient client = new MyInterfaceClient(binding, endpoint);

client.WSFunctionCompleted += (object sender, WSFunctionCompletedEventArgs e) => {
    //access e.Result here
};

client.WSFunctionAsync("XXXXXX");

Your mileage may vary. Let me know if this works.

你的旅费可能会改变。让我知道这个是否奏效。

#2


1  

 var binding = new BasicHttpBinding();
        binding.ProxyAddress = new Uri(string.Format("http://{0}:{1}", proxyAddress, proxyPort));
        binding.UseDefaultWebProxy = false;
        binding.Security.Mode = BasicHttpSecurityMode.Transport;
        binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
        binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.Basic;

        var endpoint = new EndpointAddress("serviceadress");

        var authenticationClient = new WOKMWSAuthenticateClient(binding, endpoint);
        authenticationClient.ClientCredentials.UserName.UserName = username;
        authenticationClient.ClientCredentials.UserName.Password = password;

if you want to run it on your local you should this code.

如果你想在你的本地运行它,你应该这个代码。

 ServicePointManager.Expect100Continue = false;

#3


0  

Very easy and simple way to call WCF :

调用WCF非常简单方便:

            BasicHttpBinding myBinding = new BasicHttpBinding();
            EndpointAddress myEndpoint = new EndpointAddress("http://localhost:3283/Service1.svc");

            myBinding.ReaderQuotas.MaxArrayLength = int.MaxValue;
            myBinding.MaxBufferSize = int.MaxValue;
            myBinding.MaxReceivedMessageSize = int.MaxValue;

            ChannelFactory<ITestAPI> myChannelFactory = new ChannelFactory<ITestAPI>(myBinding, myEndpoint);

            ITestAPI instance = myChannelFactory.CreateChannel();

            Test data = new Test();
            data.helloName = name;
            data= instance.GetMyName(data);

            myChannelFactory.Close();

#1


8  

The easiest way to access a service via a BasicHttpBinding is to generate the client code from SlSvcUtil.exe, which is a silverlight utility application.

通过BasicHttpBinding访问服务的最简单方法是从SlSvcUtil.exe生成客户端代码,这是一个silverlight实用程序。

SLsvcUtil.exe /directory:C:\users\me\Desktop http://URL.svc

That should create a MyInterfaceClient class inside of the file it generates.

这应该在它生成的文件中创建一个MyInterfaceClient类。

Then in your code you can do:

然后在您的代码中,您可以:

var binding = new BasicHttpBinding() {
    Name = "BindingName",
    MaxBufferSize = 2147483647,
    MaxReceivedMessageSize = 2147483647
};

var endpoint = new EndpointAddress("URL.svc");

MyInterfaceClient client = new MyInterfaceClient(binding, endpoint);

client.WSFunctionCompleted += (object sender, WSFunctionCompletedEventArgs e) => {
    //access e.Result here
};

client.WSFunctionAsync("XXXXXX");

Your mileage may vary. Let me know if this works.

你的旅费可能会改变。让我知道这个是否奏效。

#2


1  

 var binding = new BasicHttpBinding();
        binding.ProxyAddress = new Uri(string.Format("http://{0}:{1}", proxyAddress, proxyPort));
        binding.UseDefaultWebProxy = false;
        binding.Security.Mode = BasicHttpSecurityMode.Transport;
        binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
        binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.Basic;

        var endpoint = new EndpointAddress("serviceadress");

        var authenticationClient = new WOKMWSAuthenticateClient(binding, endpoint);
        authenticationClient.ClientCredentials.UserName.UserName = username;
        authenticationClient.ClientCredentials.UserName.Password = password;

if you want to run it on your local you should this code.

如果你想在你的本地运行它,你应该这个代码。

 ServicePointManager.Expect100Continue = false;

#3


0  

Very easy and simple way to call WCF :

调用WCF非常简单方便:

            BasicHttpBinding myBinding = new BasicHttpBinding();
            EndpointAddress myEndpoint = new EndpointAddress("http://localhost:3283/Service1.svc");

            myBinding.ReaderQuotas.MaxArrayLength = int.MaxValue;
            myBinding.MaxBufferSize = int.MaxValue;
            myBinding.MaxReceivedMessageSize = int.MaxValue;

            ChannelFactory<ITestAPI> myChannelFactory = new ChannelFactory<ITestAPI>(myBinding, myEndpoint);

            ITestAPI instance = myChannelFactory.CreateChannel();

            Test data = new Test();
            data.helloName = name;
            data= instance.GetMyName(data);

            myChannelFactory.Close();