如何以编程的方式创建BasicHttpBinding?

12

我有以下的代码:

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,但是我从来没有得到过填充结果。

我做错了什么?

先感谢您的帮助!


那个不会编译,小写字母a是字符串类型,大写字母A是布尔类型。 - Wiktor Zychla
1
结果命名空间可能是罪魁祸首之一,但我会从使用 HTTP 调试器嗅探流量开始。 - Wiktor Zychla
1
使用 Fiddler 我可以看到我可以完美地访问 WS!但结果是罪魁祸首...总是 null... - briba
1
你能展示一下你对 IMyInterface 接口的实现吗? - Millie Smith
但它不会返回异常吗?实际上我从WS复制了DataContract... =/ ...猜想这是正确的。 - briba
显示剩余5条评论
3个回答

9
通过使用Silverlight实用程序SlSvcUtil.exe生成客户端代码是访问基于BasicHttpBinding的服务的最简单方法。
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");

你的结果可能会有所不同。如果这个方法有效,请让我知道。


2
利用 SvcUtil 可以产生类似的结果。但我认为整个问题都是关于“手动”操作的。 - Mare Infinitus
我想这至少会给他一些代码来阅读以找出问题所在。虽然我不明白为什么他需要在没有SvcUtil的情况下这样做。 - Millie Smith
猜想这就是我需要的!谢谢你,米莉! - briba

3
 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;

0

调用 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();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接