我正在尝试实现WCF Rest服务而不修改App.config文件.
我的服务界面如下所示:
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "/teststr/?string={aStr}")]
string TestString(string aStr);
}
Run Code Online (Sandbox Code Playgroud)
服务实现非常基础:
public class TestService : IService
{
public string TestString(string aStr = null)
{
Console.WriteLine("The Test() method was called at {0}"
+ "\n\rThe given string was {1}\n\r"
, DateTime.Now.ToString("H:mm:ss")
, aStr);
return aStr;
}
}
Run Code Online (Sandbox Code Playgroud)
我的主要程序运行一切:
// Step 1 Create a URI to serve as the base address.
Uri baseAddress = new Uri("http://localhost:8000/ServiceSample/");
// Step 2 Create a ServiceHost instance
ServiceHost selfHost = new ServiceHost(typeof(TestService), baseAddress);
try
{
// Step 3 Add a service endpoint.
selfHost.AddServiceEndpoint(typeof(IService), new WebHttpBinding(),
"TestService");
// Step 4 Enable metadata exchange.
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
selfHost.Description.Behaviors.Add(smb);
// Step 5 Start the service.
selfHost.Open();
Console.WriteLine("The service is ready.");
Console.WriteLine("Press <ENTER> to terminate service.");
Console.ReadLine();
// Close the ServiceHostBase to shutdown the service.
selfHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine("An exception occurred: {0}", ce.Message);
selfHost.Abort();
}
Run Code Online (Sandbox Code Playgroud)
当我运行它并输入以下URL:
http://localhost:8000/ServiceSample/TestService/teststr/?string=thisisatest
Run Code Online (Sandbox Code Playgroud)
我收到这条消息:
// The message with To '...' cannot be processed at the receiver, due to an
// AddressFilter mismatch at the EndpointDispatcher. Check that the sender
// and receiver's EndpointAddresses agree.
Run Code Online (Sandbox Code Playgroud)
我看过类似的SO问题,建议我加入<webHttp/>水煤浆(但不Step 4这样做了吗?),有人说加[ServiceBehavior ..] .这些都没有奏效,我没有发现相关的SO问题有用.
我是否需要修改App.config文件?我在这做错了什么?
Toa*_*ows 12
我意识到上面的评论中有一个答案,但我会尝试说明我在尝试找到问题的解决方案时学到了什么,因为这是通过搜索错误消息在Google中出现的第一页.
我试图用JavaScript调用第三方SOAP Web服务(我知道很糟糕),而且我遇到了这个问题.
在SOAP 1.2服务我打电话要求我把:ToSOAP在寻址头<soap:Header>的<soap:Envelope>.在完成此操作以及添加:Action我从WSDL获得的内容之后,我才是金色的.
SOAP Header最终看起来像什么:
<soap:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
<wsa:To>(WebService URL)</wsa:To>
<wsa:Action>(Action from WebService WSDL)</wsa:Action>
</soap:Header>
Run Code Online (Sandbox Code Playgroud)
有用的网站:
| 归档时间: |
|
| 查看次数: |
8570 次 |
| 最近记录: |