修改 我已经为我尝试找到解决方案的代码添加了编辑,但错误仍然相同。
以下整个文本块的重点是:
WCF服务器无法与Objective-C客户端通信
设置[OperationContract(ProtectionLevel = ProtectionLevel.None)]
,[ServiceContract(Name = "IServer", Namespace = "http://tempuri.org/")]
和[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
无法解决错误消息。详细配置如下所示。
=============================================== =================================== 所以我创建了一个服务并将其上传到服务器
web.config文件如下:
<?xml version="1.0"?>
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<authentication mode="Windows"/>
<customErrors mode="Off"></customErrors>
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
</system.web>
<system.webServer>
<directoryBrowse enabled="true"/>
</system.webServer>
<system.serviceModel>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="false" />
<services>
<service behaviorConfiguration="AppComService.Service1Behavior" name="AppComService.Service">
<endpoint address="" binding="basicHttpBinding" contract="AppComService.ComService">
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="AppComService.Service1Behavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
这是服务代码:
[ServiceContract(Name = "IServer", Namespace = "http://tempuri.org/")]
public interface ComService
{
[OperationContract(ProtectionLevel = ProtectionLevel.None)]
string Foo();
// TODO: Add your service operations here
}
...
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
public class Service : ComService
{
public string Foo()
{
return "foo!";
}
}
客户端在Objective-c中完成,配置如下:
NSString *soapMessage = [NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<SOAP-ENV:Envelope \n"
"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" \n"
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n"
"xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" \n"
"SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" \n"
"xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"> \n"
"<SOAP-ENV:Body> \n"
"<Login xmlns=\"http://tempuri.org/\"><username>username</username><password>password</password>"
"</Login> \n"
"</SOAP-ENV:Body> \n"
"</SOAP-ENV:Envelope>"];
NSURL *url = [NSURL URLWithString:@"http://api.tagastic.com/Service.svc/foo"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%i", (int)[soapMessage length]];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://tempuri.org/ComService/foo" forHTTPHeaderField:@"Soapaction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
当我们尝试调用Foo()方法时,我们得到:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<s:Fault>
<faultcode xmlns:a="http://schemas.microsoft.com/ws/2005/05/addressing/none">
a:ActionNotSupported
</faultcode>
<faultstring xml:lang="hr-HR">
The message with Action 'http://tempuri.org/ComService/foo' cannot be processed at the
receiver, due to a ContractFilter mismatch at the EndpointDispatcher. This may be
because of either a contract mismatch (mismatched Actions between sender and receiver)
or a binding/security mismatch between the sender and the receiver. Check that sender
and receiver have the same contract and the same binding (including security
requirements, e.g. Message, Transport, None).
</faultstring>
</s:Fault>
</s:Body>
</s:Envelope>
有谁知道如何解决这个问题?
我已经完成了有关此事的大部分主题,但所有解决方案都未能解决我们的具体情况。
答案 0 :(得分:0)
所以最后我需要做一些事情: 首先将web调用添加到metoods:
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "Foo")]
然后我将元素添加到web.config:
<service behaviorConfiguration="AppComService.Service1Behavior" name="AppComService.Service">
<endpoint address="../Service.svc"
binding="webHttpBinding"
contract="AppComService.ComService"
behaviorConfiguration="webBehaviour" />
</service>
...
<endpointBehaviors>
<behavior name="webBehaviour">
<webHttp/>
</behavior>
</endpointBehaviors>
就是这样。客户现在可以访问该服务。