如何以编程方式创建 BasicHttpBinding?
Posted
技术标签:
【中文标题】如何以编程方式创建 BasicHttpBinding?【英文标题】:How to programmatically create a BasicHttpBinding? 【发布时间】:2013-11-15 11:22:42 【问题描述】:我必须遵循以下代码:
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实现的接口..例如:
[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,但我永远无法填写 Result。
我做错了什么?
提前致谢!
【问题讨论】:
编译不了,小a是字符串类型,大A是bool类型。 是因为我改了名字...但实际上它编译并且WS接收到消息...但是我无法得到结果.. 结果命名空间是可能的罪魁祸首之一,但我将从使用 http 调试器嗅探流量开始。 使用 Fiddler 我可以看到我可以完美地到达 WS!但是 Result 是有罪的......总是为空...... 你能展示你的IMyInterface实现吗? 【参考方案1】:通过BasicHttpBinding
访问服务的最简单方法是从 Silverlight 实用程序应用程序 SlSvcUtil.exe 生成客户端代码。
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");
您的里程可能会有所不同。让我知道这个是否奏效。
【讨论】:
利用 SvcUtil 将产生类似的结果。但我认为整个问题在于“手工”完成。 我认为它至少会给他代码来阅读以解决问题。我不明白为什么他需要在没有 SvcUtil 的情况下这样做。 猜猜这是我需要的!谢谢米莉!!【参考方案2】: 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;
如果您想在本地运行它,您应该使用此代码。
ServicePointManager.Expect100Continue = false;
【讨论】:
【参考方案3】:调用 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();
【讨论】:
以上是关于如何以编程方式创建 BasicHttpBinding?的主要内容,如果未能解决你的问题,请参考以下文章