gpt4 book ai didi

c# - 如何以编程方式创建 BasicHttpBinding?

转载 作者:可可西里 更新时间:2023-11-01 08:25:30 27 4
gpt4 key购买 nike

我必须遵循以下代码:

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 ();
}
}

“IMyInterface”是我的 WS 实现的接口(interface)。例如:

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

[OperationContract]
Result WsFunction2 (string param);

[OperationContract]
Result WsFunction3 (string param);
}

它返回这样的东西:

[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; }
}
}

当我运行这段代码时,我可以到达 WS,但我永远无法填写结果。

我做错了什么?

提前致谢!

最佳答案

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

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

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

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

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");

您的里程可能会有所不同。让我知道这个是否奏效。

关于c# - 如何以编程方式创建 BasicHttpBinding?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19774540/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com