使用在Android KSoap2的Mono上运行的WCF Soap服务
本文关键字:运行 WCF Soap 服务 Mono Android KSoap2 | 更新日期: 2023-09-27 18:24:26
我正在做一个Android/C#项目。我需要做的是有一个WCF soap服务,它可以在Windows或Linux(Mono)上运行。
它在Windows上运行良好,我可以从Visual Studio中提供的WCF测试客户端访问Mono上的WCF服务,它运行良好,但当使用KSOAP2访问android时,我收到错误HTTP Request Failed, HTTP status: 415
以下是如何启动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();
}
soapServerURL是http://192.168.1.74:8000/CritiMon
。
以下是我如何尝试使用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());
}
我不知道我能做些什么来让它在安卓的Mono上运行。
首先,您需要捕获连线上的实际SOAP请求。您可以使用Fiddler或SoapUI来实现这一点——它们都充当传递本地请求的代理,允许您检查实际的XML请求是否存在异常。通过这样做,你可能会发现一些显而易见的东西,或者你至少可以用更多信息更新你的问题。
接下来,在没有任何进一步信息的情况下,我只能提供一个与非.NET应用程序的WCF服务对话的难忘体验:
WCF指定了它所期望的XML请求,但它实际上要求对象属性按特定顺序排列。这可以是数据契约中声明的顺序,也可以是隐含的字母顺序。无论哪种方式,如果你不按指定的顺序提供对象属性,你都会被刺痛,事情就无法进行。