Make this WCF client code work on Mono and MonoTou

2019-02-16 03:57发布

问题:

I am trying to get the following piece of code to work in a Mono console app and as the ultimate goal in a MonoTouch application. The code works fine under VS2008 using a Windows console app. When running it as a Mono console app on the Mac, I get

Unhandled Exception: System.NotImplementedException: The requested feature is not implemented. at System.ServiceModel.Channels.SecurityBindingElement.CanBuildChannelFactory[IDuplexChannel] (System.ServiceModel.Channels.BindingContext context) [0x00000] in :0

In MonoTouch, I receive this error:

Unhandled Exception: System.ServiceModel.FaultException: An error occurred when verifying security for the message. at System.ServiceModel.MonoInternal.ClientRuntimeChannel.Request (System.ServiceModel.Description.OperationDescription od, System.Object[] parameters) [0x00188] in /Developer/MonoTouch/Source/mono/mcs/class/System.ServiceModel/System.ServiceModel/ClientRuntimeChannel.cs:545

Can somebody eplxain what is wrong? There is no app.config involved to keep things simple.

using System;
using MonoAPI3.BLAPI3Session;
using Brainloop.ServiceLibrary.DataModel;
using System.ComponentModel;
using System.ServiceModel;

public static void Main()
{
System.Net.ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) => { return true; };

var oClient = new SessionServiceClient( new BasicHttpBinding( BasicHttpSecurityMode.TransportWithMessageCredential )
            {
                CloseTimeout = new TimeSpan( 0, 0, 10 ),
                OpenTimeout = new TimeSpan( 0, 1, 0 ),
                ReceiveTimeout = new TimeSpan( 0, 1, 0 ),
                SendTimeout = new TimeSpan( 0, 1, 0 ),
                AllowCookies = false,
                BypassProxyOnLocal = false,
                HostNameComparisonMode = HostNameComparisonMode.StrongWildcard,
                MaxBufferSize = 65536,
                MaxBufferPoolSize = 524288,
                MaxReceivedMessageSize = 65536,
                MessageEncoding = WSMessageEncoding.Text,
                TextEncoding = System.Text.Encoding.UTF8,
                TransferMode = TransferMode.Buffered,
                UseDefaultWebProxy = true,
                ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas()
                {
                    MaxDepth = 32,
                    MaxStringContentLength = 8192,
                    MaxArrayLength = 16384,
                    MaxBytesPerRead = 4096,
                    MaxNameTableCharCount = 16384
                }

            },
            new EndpointAddress( "https://service.myserver.com/Service/V3/Session.svc" ) );

            oClient.ClientCredentials.UserName.UserName = "user@domain.com";
            oClient.ClientCredentials.UserName.Password = "pwd";

            SessionInfo oInfo = oClient.StartSession();

            Console.WriteLine( "SESSION HASH: " + oInfo.SessionHash );
}

回答1:

The problem line of code is:

BasicHttpSecurityMode.TransportWithMessageCredential

Mono does not support WS-Security. Basically there are many options on BasicHttpSecurityMode which will not work. If you require Message Credentials, then I'm afraid this code cannot be made to work. Here is a related link:

http://lists.ximian.com/pipermail/mono-bugs/2010-January/096972.html



回答2:

I recommend you to download the attachment from this link.

I was successful with getting it to work. I set the Web project on Windows machine under IIS 7 with only basic authentication enabled and .NET authorization to a specific user. Then I just run the console app under MonoDevelop on my Mac machine and it worked without any errors.

The only difference I can see between the Mono console app and MonoTouch app is this line:

binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;

In the MonoTouch app the binding.Security.Transport property is not generated.