WCF 客户端使用带有 Soap 标头的 ASMX Web 服务

发布于 2024-09-25 07:24:07 字数 2196 浏览 1 评论 0原文

我有一个 ASMX Web 服务,需要一个肥皂头和一个通过服务引用 (WCF) 使用该服务的客户端应用程序。

服务器:

[WebService(Namespace = "http://myserviceurl.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service1 : System.Web.Services.WebService
{
    public MyCustomSoapHeader MyHeader;

    [WebMethod]
    [SoapHeader("MyHeader", Direction = SoapHeaderDirection.In)]
    public string MyMethod()
    {
        if(MyHeader.SomeProperty == false)
        {
             return "error";
        }
        return "success";
    }

    public class MyCustomSoapHeader: SoapHeader
    {
        public bool SomeProperty { get; set; }
    }
}

客户端:

public class MyClient
{
    var address = new EndpointAddress(_myServerUrl)

    var binding = new BasicHttpBinding();
    binding.Name = "SoapBinding";
    binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
    binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;
    binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
    binding.ReceiveTimeout = TimeSpan.FromSeconds(_timeout);

    Service1SoapClient client = new Service1SoapClient(binding, address);

    string expected = client.MyMethod(new MyCustomSoapHeader(){SomeProperty = true});
}

堆栈跟踪:

System.ServiceModel.FaultException: Server was unable to process request. ---> Computer name could not be obtained.

Server stack trace: 
   at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

如何在仍在使用 WCF 的客户端上修复此问题?我无法更改服务器代码,并且需要在客户端上使用 WCF,无法添加 Web 引用。

I have an ASMX web service that has requires a soap header and a client app consuming this service via a service reference (WCF).

Server:

[WebService(Namespace = "http://myserviceurl.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service1 : System.Web.Services.WebService
{
    public MyCustomSoapHeader MyHeader;

    [WebMethod]
    [SoapHeader("MyHeader", Direction = SoapHeaderDirection.In)]
    public string MyMethod()
    {
        if(MyHeader.SomeProperty == false)
        {
             return "error";
        }
        return "success";
    }

    public class MyCustomSoapHeader: SoapHeader
    {
        public bool SomeProperty { get; set; }
    }
}

Client:

public class MyClient
{
    var address = new EndpointAddress(_myServerUrl)

    var binding = new BasicHttpBinding();
    binding.Name = "SoapBinding";
    binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
    binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;
    binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
    binding.ReceiveTimeout = TimeSpan.FromSeconds(_timeout);

    Service1SoapClient client = new Service1SoapClient(binding, address);

    string expected = client.MyMethod(new MyCustomSoapHeader(){SomeProperty = true});
}

Stack trace:

System.ServiceModel.FaultException: Server was unable to process request. ---> Computer name could not be obtained.

Server stack trace: 
   at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

How can I fix this on the client side still using WCF? I am unable to change the server code and I need to use WCF on the client, I can't add a web reference.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼角的笑意。 2024-10-02 07:24:07

基于我刚刚回答的另一个问题(关于读出 SOAP 标头),此方法应该可以满足您在从 WCF 客户端调用 ASMX 服务时包含 SOAP 标头的要求:

Service1SoapClient client = new Service1SoapClient(binding, address);

using(OperationContextScope scope = new OperationContextScope(client.InnerChannel))
{
    // set the message in header
    MessageHeader header = MessageHeader.CreateHeader("MyHeader", "urn:Sample-NS", "Some Value");
    OperationContext.Current.OutgoingMessageHeaders.Add(header); 

    string expected = client.MyMethod(new MyCustomSoapHeader(){SomeProperty = true});
}

我希望这可行 - 我真的没有基础设施现在就在手头测试一下......

Based on another question I just answered (about reading out SOAP headers), this approach should work for your requirement of including a SOAP header when calling an ASMX service from a WCF client:

Service1SoapClient client = new Service1SoapClient(binding, address);

using(OperationContextScope scope = new OperationContextScope(client.InnerChannel))
{
    // set the message in header
    MessageHeader header = MessageHeader.CreateHeader("MyHeader", "urn:Sample-NS", "Some Value");
    OperationContext.Current.OutgoingMessageHeaders.Add(header); 

    string expected = client.MyMethod(new MyCustomSoapHeader(){SomeProperty = true});
}

I hope this works - I don't really have the infrastructure at hand to test this right now...

紫瑟鸿黎 2024-10-02 07:24:07

此时您的问题看起来根本与 SOAP 标头无关...“无法获取计算机名称”错误来自其他地方。

您在客户端上指定什么作为服务 URL?服务与客户端运行在同一台机器上,还是不同的机器上?该服务是否可以与其他非 WCF 客户端一起使用?

It doesn't look like your problem at this point is related with the SOAP Header at all... that "Computer name could not be obtained" error comes from somewhere else.

What are you specifying on the client as the service URL? Is the service running on the same machine as the client, or a different one? Is the service working with other, non-WCF clients?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文