如何使用WCF绑定的app.config并以正确的方式以编程方式将其转换为绑定

时间:2012-04-18 11:07:38

标签: c# .net wcf soap

我有以下绑定,在我创建客户端时工作正常。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="Assets_AssetsPort" closeTimeout="00:01:00" openTimeout="00:01:00"
                    receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
                    bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Mtom" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="None">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
                <binding name="AssetsPortBinding" closeTimeout="00:01:00" openTimeout="00:01:00"
                    receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
                    bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="None">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://osi-tm/services/Assets" binding="basicHttpBinding"
                bindingConfiguration="Assets_AssetsPort" contract="InterplayWS.AssetsPortType"
                name="AssetsPort" />
        </client>
    </system.serviceModel>
</configuration>

但是现在我需要在没有任何配置文件的情况下配置它。所以我试图做到以下几点:

    BasicHttpBinding basicBinding = new BasicHttpBinding()
    {
        MaxReceivedMessageSize = 2147483647,
        MaxBufferPoolSize = 2147483647
    };
    basicBinding.MessageEncoding = WSMessageEncoding.Mtom;


    var endpoint = new EndpointAddress(new Uri(String.Format("http://{0}/services/Assets?wsdl", Connection.InterplayHost)));

    var client = new AssetClient(basicBinding, endpoint);

当我运行时,我收到错误

<Exception>
<ExceptionType>System.ServiceModel.CommunicationException, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType>
<Message>Unrecognized message version.</Message>
<StackTrace>
at System.ServiceModel.Channels.ReceivedMessage.ReadStartEnvelope(XmlDictionaryReader reader)
at System.ServiceModel.Channels.BufferedMessage..ctor(IBufferedMessageData messageData, RecycledMessageState recycledMessageState, Boolean[] understoodHeaders)
at System.ServiceModel.Channels.MtomMessageEncoder.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(Exception&amp; requestException)
at System.ServiceModel.Channels.HttpChannelFactory.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)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&amp; msgData, Int32 type)
at DDMEService.InterplayReference.AssetsPortType.GetChildren(GetChildrenRequest request)
at DDMEService.InterplayReference.AssetsPortTypeClient.DDMEService.InterplayReference.AssetsPortType.GetChildren(GetChildrenRequest request)
at DDMEService.InterplayReference.AssetsPortTypeClient.GetChildren(UserCredentialsType UserCredentials, GetChildrenType GetChildren1)
at DDMEService.Classes.AvidCommand.PopulateGenericAssets(String UNCHostname, String UNCDirectoryPath)
at DDMEService.Classes.CommandManager.PrepareAvidList(String HostName, String DirectoryPath, Boolean SearchSubdirectory, Boolean RenameToDirectory, String FileFilter, String ExtensionFilter, String InterplayHost, String InterplayWorkgroup, String Catalog, String LocatorSequenceStartWords, String LocatorSequenceEndWords, String Username, String Password)
at DDMEService.Classes.CommandManager.Execute()
at DDMEService.ClientHandler.ProcessCommand(String ClientIP, String Command)
at DDMEService.ClientHandler.Process(Object O)
at System.Threading._ThreadPoolWaitCallback.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallbackInternal(_ThreadPoolWaitCallback tpWaitCallBack)
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback(Object state)
</StackTrace>
<ExceptionString>System.ServiceModel.CommunicationException: Unrecognized message version.</ExceptionString>
</Exception>

无法使用肥皂请求

--uuid:4475db34-2787-4220-b800-15c91678a4be+id=2
Content-ID: <http://tempuri.org/0>
Content-Transfer-Encoding: 8bit
Content-Type: application/xop+xml;charset=utf-8;type="text/xml"

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Header><h:UserCredentials xmlns:h="http://avid.com/interplay/ws/assets/types" xmlns="http://avid.com/interplay/ws/assets/types" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><Username>Administrator</Username><Password>avid</Password></h:UserCredentials><ActivityId CorrelationId="f29e64f8-1568-4683-b196-d53bd97a2cec" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">13063b98-becc-416a-89ec-23a23498ed21</ActivityId></s:Header><s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><GetChildren xmlns="http://avid.com/interplay/ws/assets/types"><InterplayURI>interplay://A51WG6/Catalogs/000</InterplayURI></GetChildren></s:Body></s:Envelope>
--uuid:4475db34-2787-4220-b800-15c91678a4be+id=2--

工作肥皂请求

--uuid:f1481ad5-def1-42bf-a3ca-f9cff5eb856b+id=2
Content-ID: <http://tempuri.org/0>
Content-Transfer-Encoding: 8bit
Content-Type: application/xop+xml;charset=utf-8;type="text/xml"

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Header><h:UserCredentials xmlns:h="http://avid.com/interplay/ws/assets/types" xmlns="http://avid.com/interplay/ws/assets/types" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><Username>Administrator</Username><Password>avid</Password></h:UserCredentials></s:Header><s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><GetChildren xmlns="http://avid.com/interplay/ws/assets/types"><InterplayURI>interplay://WG5C/Catalogs/Sundance</InterplayURI></GetChildren></s:Body></s:Envelope>
--uuid:f1481ad5-def1-42bf-a3ca-f9cff5eb856b+id=2--

我如何正确地形成这个以使我的服务不起作用?

3 个答案:

答案 0 :(得分:2)

为什么要创建元素然后推动mtom?你已经有一个文本编码器,所以mtom将是第二个编码器。请改用:

basicBinding.messageEncoding = WSMessageEncoding.Mtom

答案 1 :(得分:0)

我不得不从http:// {0} / services / Assets中删除?wsdl?wsdl

答案 2 :(得分:-1)

要在代码中创建等效绑定,只需将BasicHttpBinding实例上的属性设置为配置文件中指定的值即可。

从快速扫描代码看起来您​​可以删除创建MtomMessageEncodingBindingElement实例的代码,并将basicBinding上的MessageEncoding属性设置为Mtom。

相关问题