简体   繁体   English

从Android KSoap2开始在Mono上运行WCF Soap Service

[英]Consuming WCF Soap Service running on Mono from Android KSoap2

I am working on a Android/C# project. 我正在开发一个Android / C#项目。 What I need to be able to do is have a WCF soap service which can either run on Windows or Linux (Mono). 我需要做的是有一个WCF soap服务,可以在Windows或Linux(Mono)上运行。

It's working fine on Windows and I can access the WCF Service on Mono from the WCF Test Client provided in Visual Studio and it works fine but when accessing android using KSOAP2 I get the error HTTP Request Failed, HTTP status: 415 它在Windows上工作正常,我可以从Visual Studio中提供的WCF测试客户端访问Mono上的WCF服务,它工作正常但是当使用KSOAP2访问android时,我收到错误HTTP Request Failed, HTTP status: 415

Below is how the soap service is started 以下是soap服务的启动方式

string methodInfo = classDetails + MethodInfo.GetCurrentMethod().Name;
            try
            {
                if (Environment.GetEnvironmentVariable("MONO_STRICT_MS_COMPLIANT") != "yes")
                {
                    Environment.SetEnvironmentVariable("MONO_STRICT_MS_COMPLIANT", "yes");
                }
                if (String.IsNullOrEmpty(soapServerUrl))
                {
                    string message = "Not starting Soap Server: URL or Port number is not set in config file";
                    library.logging(methodInfo, message);
                    library.setAlarm(message, CommonTasks.AlarmStatus.Medium, methodInfo);
                    return;
                }
                Console.WriteLine("Soap Server URL: {0}", soapServerUrl);
                baseAddress = new Uri(soapServerUrl);
                host = new ServiceHost(soapHandlerType, baseAddress);
                BasicHttpBinding basicHttpBinding = new BasicHttpBinding();

                //basicHttpBinding.Namespace = "http://tempuri.org/";



                var meta = new ServiceMetadataBehavior()
                {
                    HttpGetEnabled = true,
                    HttpGetUrl = new Uri("", UriKind.Relative),
                    //HttpGetBinding = basicHttpBinding,
                };
                //meta.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;

                host.Description.Behaviors.Add(meta);

                host.AddServiceEndpoint(soapManagerInterface, basicHttpBinding, soapServerUrl);
                host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex");

                var debugBehaviour = new ServiceDebugBehavior()
                {
                    HttpHelpPageEnabled = true,
                    HttpHelpPageUrl = new Uri("", UriKind.Relative),
                    IncludeExceptionDetailInFaults = true,
                    //HttpHelpPageBinding = basicHttpBinding,
                };

                host.Description.Behaviors.Remove(typeof(ServiceDebugBehavior));
                host.Description.Behaviors.Add(debugBehaviour);
                host.Opened += new EventHandler(host_Opened);
                host.Faulted += new EventHandler(host_Faulted);
                host.Closed += new EventHandler(host_Closed);
                host.UnknownMessageReceived += new EventHandler<UnknownMessageReceivedEventArgs>(host_UnknownMessageReceived);
                host.Open();
            }

The soapServerURL is http://192.168.1.74:8000/CritiMon . soapServerURL是http://192.168.1.74:8000/CritiMon

Below is how I am trying to call the soap service from android using KSOAP2. 以下是我尝试使用KSOAP2从android调用soap服务的方法。

final String soapAction = "http://tempuri.org/ISoapInterface/testSoapFunction";
        final String namespace = "http://tempuri.org/";
        final String methodName = "testSoapFunction";
        final String url = "http://192.168.1.74:8000/CritiMon?wsdl";
        String resultData = "";

        new Thread(new Runnable() {

            @Override
            public void run() {
                SoapSerializationEnvelope envelope = null;
                try
                {
                //String resultData = "";

                    SoapObject request = new SoapObject(namespace, methodName);
                    //request.addProperty("firstName", "Chris");
                    //request.addProperty("lastName", "Board");
                    envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                    envelope.dotNet = true;
                    envelope.setOutputSoapObject(request);

                    HttpTransportSE http = new HttpTransportSE(url);

                    http.call(soapAction, envelope);
                    SoapPrimitive result = (SoapPrimitive)envelope.getResponse();

                    String resultData = result.toString();
                    Log.d("Soap Result", resultData);

                }
                catch (Exception ex)
                {
                    Log.e("Soap Error 2", ex.getMessage());
}

I have no idea what I can do to make this work on Mono with Android. 我不知道我能做些什么才能在Android上使用Mono进行这项工作。

Firstly, you want to catch the actual SOAP request on the wire. 首先,您希望在线路上捕获实际的SOAP请求。 You can do this using Fiddler or SoapUI - they both act as proxies through which local requests are passed, allowing you to inspect the actual XML request for anomalies. 您可以使用Fiddler或SoapUI执行此操作 - 它们都充当代理,通过该代理传递本地请求,允许您检查异常的实际XML请求。 You might discover something obvious by doing this, or you can at least update your question with more information. 您可以通过这样做发现明显的事情,或者至少可以通过更多信息更新您的问题。

Following that, and without any further information, I can only offer a memorable experience with talking to WCF services from non-.NET applications: 接下来,在没有任何进一步信息的情况下,我只能通过非.NET应用程序与WCF服务交谈,提供难忘的体验:

WCF specifies the XML request that it expects, but it actually requires object properties to be in a specific order. WCF指定它期望的XML请求,但它实际上要求对象属性按特定顺序排列。 This can be a declared order in the datacontract, or it can be an implicit alphabetical order. 这可以是datacontract中的声明顺序,也可以是隐式字母顺序。 Either way, if you don't provide object properties in the specified order you will be stung and things won't work. 无论哪种方式,如果您不按指定的顺序提供对象属性,您将被蜇,事情将无法工作。

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

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