简体   繁体   English

WCF名称空间前缀

[英]WCF namespace prefix

I am trying to consume what I understand to be an older style ASMX web service from a third party provider, but want to use WCF since that is the recommended approach for newer style applications. 我正在尝试使用第三方提供程序提供的我认为是较旧样式的ASMX Web服务,但是要使用WCF,因为这是较新样式应用程序的推荐方法。

When I add a service reference to my application as a Web Reference (click the Advanced button in the Add Service Reference dialog and choose Add Web Reference), I can successfully consume the web service. 将服务引用作为Web引用添加到应用程序时(单击“添加服务引用”对话框中的“高级”按钮,然后选择“添加Web引用”),我可以成功使用Web服务。 Everything works. 一切正常。 This, however, is the older style method of setting up the web service, from what I understand. 但是,据我了解,这是设置Web服务的较旧样式的方法。

What I want is to do it via WCF, so I went about adding a service reference without doing so as a web reference (eg Add Service Reference dialog, putting in the address and clicking Go). 我想要通过WCF进行操作,所以我添加了一个服务引用,而没有将其作为Web引用(例如,“添加服务引用”对话框,输入地址并单击“转到”)。 All of my code to talk to the web service seems to work, however I keep getting the following exception: 我所有与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: 

I've played around with fiddler and it looks like a simple namespace problem that I am not sure how to fix. 我玩过提琴手,它看起来像一个简单的名称空间问题,我不确定如何解决。 Here is a message that works when setting up the service as a web reference: 这是在将服务设置为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>

And here is one that does not when setting the service up as just a service reference (not as a web reference): 这是将服务设置为仅服务参考(而不是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>

There are clearly differences between the two messages other than just the namespaces, but all I have to do in fiddler is take the message that doesn't work and change all of the s: namespaces to soap: and everything works. 显然,除了命名空间之外,这两个消息之间还存在差异,但是我在提琴手中要做的就是将不起作用的消息拿走,然后将所有s:命名空间更改为soap :,然后一切正常。 So, I am pretty sure all I have to do is change the namespace just like I did with fiddler and this should work using WCF, but I am not sure how to do that. 因此,我很确定我所要做的就是像使用fiddler一样更改名称空间,这应该可以在WCF中使用,但是我不确定如何做到这一点。

You should try using the svcutil command line tool (open a VS Command Prompt to run it), which although it is similar to the reference feature of VS is actually a completely different codebase. 您应该尝试使用svcutil命令行工具(打开VS Command Prompt来运行它),尽管它与VS的参考功能相似,但实际上是完全不同的代码库。 You can use that to generate a proxy, all the client-side types etc, just as with the service reference feature and it's pretty much reliable. 您可以使用它生成代理,所有客户端类型等,就像使用服务引用功能一样,它非常可靠。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM