WCF名称空间前缀

本文关键字:前缀 空间 WCF | 更新日期: 2023-09-27 18:04:48

我正在尝试使用来自第三方提供商的旧风格ASMX web服务,但希望使用WCF,因为这是新风格应用程序的推荐方法。

当我将一个服务引用作为Web引用添加到我的应用程序时(单击"添加服务引用"对话框中的高级按钮并选择"添加Web引用"),我可以成功地使用该Web服务。一切工作。然而,据我所知,这是设置web服务的老式方法。

我想要的是通过WCF来做,所以我去添加一个服务引用,而不像web引用那样做(例如添加服务引用对话框,输入地址并点击Go)。我所有的代码与web服务交谈似乎工作,但我一直得到以下异常:

System.ServiceModel.CommunicationException was unhandled by user code
  HResult=-2146233087
  Message=Unrecognized message version.
  Source=mscorlib
  StackTrace:
    Server stack trace: 
       at System.ServiceModel.Channels.ReceivedMessage.ReadStartEnvelope(XmlDictionaryReader reader)
       at System.ServiceModel.Channels.BufferedMessage..ctor(IBufferedMessageData messageData, RecycledMessageState recycledMessageState, Boolean[] understoodHeaders, Boolean     understoodHeadersModified)
       at System.ServiceModel.Channels.BufferedMessage..ctor(IBufferedMessageData messageData, RecycledMessageState recycledMessageState)
       at System.ServiceModel.Channels.TextMessageEncoderFactory.TextMessageEncoder.ReadMessage(ArraySegment`1 buffer, BufferManager bufferManager, String contentType)
       at System.ServiceModel.Channels.MessageEncoder.ReadMessage(Stream stream, BufferManager bufferManager, Int32 maxBufferSize, String contentType)
       at System.ServiceModel.Channels.HttpInput.ReadChunkedBufferedMessage(Stream inputStream)
       at System.ServiceModel.Channels.HttpInput.ParseIncomingMessage(HttpRequestMessage httpRequestMessage, Exception& requestException)
       at System.ServiceModel.Channels.HttpInput.ParseIncomingMessage(Exception& requestException)
       at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
       at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
       at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
       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)
    Exception rethrown at [0]: 
       at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
       at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
       at MarketVision.FulfillmentService.FulfillmentProcessors.TextWire.Processor.TextWireProxy.WS_CompanyPortType.readCompany(WSAuthentication auth)
       at MarketVision.FulfillmentService.FulfillmentProcessors.TextWire.Processor.TextWireProxy.WS_CompanyPortTypeClient.readCompany(WSAuthentication auth)
       at MarketVision.FulfillmentService.FulfillmentProcessors.TextWire.Processor.ServiceAgent.CreateCompany(String name)
       at MarketVision.FulfillmentService.FulfillmentProcessors.TextWire.Processor.TextWireFulfillmentProcessor.FulfillOrders(IEnumerable`1 orders)
       at FulfillmentService.Services.Wcf.FulfillmentService.FulfillOrders(IFulfillmentInfo fulfillmentInfo) in x:'FulfillmentService'Main'Source'Services'Wcf'FulfillmentService.cs:line 18
       at SyncInvokeFulfillOrders(Object , Object[] , Object[] )
       at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
       at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
  InnerException: 

我玩过提琴手,它看起来像一个简单的命名空间问题,我不知道如何修复。以下是将服务设置为web引用时的工作消息:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
               xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
               xmlns:tns="urn:webservices2_0_0"
               xmlns:types="urn:webservices2_0_0/encodedTypes"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
        <tns:readCompany>
            <auth href="#id1" />
        </tns:readCompany>
        <tns:WSAuthentication id="id1" xsi:type="tns:WSAuthentication">
            <api_key xsi:type="xsd:string">myApiKey</api_key>
            <code xsi:type="xsd:string">myCode</code>
            <keyword xsi:type="xsd:string">myKeyword</keyword>
            <password xsi:type="xsd:string">myPassword</password>
            <username xsi:type="xsd:string">myUsername</username>
        </tns:WSAuthentication>
    </soap:Body>
</soap:Envelope>

当将服务设置为服务引用(而不是web引用)时,这里有一个不这样做:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <q1:readCompany xmlns:q1="urn:webservices2_0_0">
            <auth href="#id1"/>
        </q1:readCompany>
        <q2:WSAuthentication id="id1" xsi:type="q2:WSAuthentication" xmlns:q2="urn:webservices2_0_0">
            <api_key xsi:type="xsd:string">myApiKey</api_key>
            <code xsi:type="xsd:string">myCode</code>
            <keyword xsi:type="xsd:string">myKeyword</keyword>
            <password xsi:type="xsd:string">myPassword</password>
            <username xsi:type="xsd:string">myUserName</username>
        </q2:WSAuthentication>
    </s:Body>
</s:Envelope>

除了名称空间之外,这两个消息之间有明显的区别,但是我在fiddler中所要做的就是获取不起作用的消息并将所有的s:名称空间更改为soap:,然后一切都可以工作。所以,我很确定我所要做的就是改变命名空间,就像我做的小提琴,这应该使用WCF工作,但我不确定如何做到这一点。

WCF名称空间前缀

您应该尝试使用svcutil命令行工具(打开VS命令提示符来运行它),尽管它类似于VS的参考特性,但实际上是一个完全不同的代码库。你可以使用它来生成代理,所有的客户端类型等,就像服务引用功能一样,它非常可靠。