微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 .NET 中的 WCF python 服务器

如何解决使用 .NET 中的 WCF python 服务器

我的目标是在 python 中创建一个 WCF 服务器,它可以在 .NET 中使用。

我首先尝试使用运行良好的 python 客户端来尝试我的服务器。但是当我尝试使用 c# 客户端时。它没有用。这是我得到的错误<soap12env:Text xml:lang="en">Requested resource \'{http://tempuri.org/}Add\' not found</soap12env:Text>

据我所知,http://tempuri.orgServiceContract 给出的认命名空间,但即使替换它,它也不起作用。

然后我尝试指定请求标头,但它也没有解决问题。

简单的python服务器:

from spyne.model.primitive import Unicode,Integer,Mandatory

from spyne import Application,ServiceBase,rpc

from spyne.model.complex import ComplexModel
from spyne.protocol.soap import Soap12
from spyne.server.wsgi import WsgiApplication


class RequestHeader(ComplexModel):
    Action = Mandatory.Unicode
    MessageID = Mandatory.Unicode
    ReplyTo = Mandatory.Unicode
    To = Mandatory.Unicode


class ExampleService(ServiceBase):
    __in_header__ = RequestHeader

    @rpc(Integer,_returns=Integer)
    def Add(ctx,a,b):
        return a+b


application = Application(
    services=[ExampleService],tns='exampleService',in_protocol=Soap12(validator='soft'),out_protocol=Soap12())

wsgi_application = WsgiApplication(application)


if __name__ == '__main__':
    import logging

    from wsgiref.simple_server import make_server

    logging.basicConfig(level=logging.DEBUG)
    logging.getLogger('spyne.protocol.xml').setLevel(logging.DEBUG)

    addr = "127.0.0.1"
    port = 12345
    logging.info(f"listening to http://{addr}:{port}")

    server = make_server(addr,port,wsgi_application)
    server.serve_forever()

简单的 C# 客户端:

class Program
{
    [ServiceContract]
    public interface ICalculator
    {
        [OperationContract]
        double Add(int n1,int n2);        
    }

    static void Main(string[] args)
    {                       
        WSHttpBinding binding = new WSHttpBinding(SecurityMode.None);
        var channel = new ChannelFactory<ICalculator>(binding);
        var uri = "http://127.0.0.1:12345/";
        var endpoint = new EndpointAddress(uri);    

        var proxy = channel.CreateChannel(endpoint);
        var result = proxy?.Add(1,2);

        Console.WriteLine(result);
        Console.ReadKey();
    }
}

来自客户端的 XML 请求:

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
  <s:Header>
    <a:Action s:mustUnderstand="1">http://tempuri.org/ICalculator/Add</a:Action>
    <a:MessageID>urn:uuid:c65324be-e271-458f-8184-ac27f1891868</a:MessageID>
    <a:ReplyTo>
      <a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
    </a:ReplyTo>
    <a:To s:mustUnderstand="1">http://127.0.0.1:12345/</a:To>
  </s:Header>
  <s:Body>
    <Add xmlns="http://tempuri.org/">
      <n1>1</n1>
      <n2>2</n2>
    </Add>
  </s:Body>
</s:Envelope>

来自服务器的 XML 响应:

<soap12env:Envelope xmlns:soap12env="http://www.w3.org/2003/05/soap-envelope">
  <soap12env:Body>
    <soap12env:Fault>
      <soap12env:Reason>
        <soap12env:Text xml:lang="en">Requested resource \'{http://tempuri.org/}Add\' not found</soap12env:Text>
      </soap12env:Reason>
      <soap12env:Role></soap12env:Role>
      <soap12env:Code>
        <soap12env:Value>soap12env:Sender</soap12env:Value>
        <soap12env:Subcode>
          <soap12env:Value>ResourceNotFound</soap12env:Value>
        </soap12env:Subcode>
      </soap12env:Code>
    </soap12env:Fault>
  </soap12env:Body>
</soap12env:Envelope>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。