简体   繁体   中英

how to go from using app.config of WCF binding and convert it to bind programmatically in correct manner

I have the following binding which works fine when I create client.

<?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>

However now I need to configure this without any config file. So I attempted to do the following:

    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);

When I run this up I get an error

<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>

not working soap request

--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--

working soap request

--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--

How can I properly form this to make this work against my service?

why do you create the elements and then push the mtom? you already have a text encoder there so the mtom would be a second encoder. use this instead:

basicBinding.messageEncoding = WSMessageEncoding.Mtom

我必须从http:// {0} / services / Assets?wsdl中删除?wsdl

To create an equivalent binding in code you just need to set the properties on your BasicHttpBinding instance to the values specified in your config file.

From a quick scan through your code it looks like you can remove the code that creates the MtomMessageEncodingBindingElement instance and set the MessageEncoding property on basicBinding to Mtom.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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