我的简单示例wcf服务正在运行,但突然之间它开始提示我在“WCF测试客户端”对话框中找到端点地址。
我不记得当我点击F5到现在显示这个“WCF测试客户端”的东西时,改变任何导致它从弹出浏览器(IE 8)的东西。
我不知道要在它提供的编辑框中输入什么,所以我尝试了“http:// localhost:4841 / RestServiceImpl.svc”(http:// localhost:4841 / RestServiceImpl.svc / xml / 123仍然可以从Visual Studio外部工作)
它接受了(“服务添加成功”显示在对话框的任务栏中),但没有做任何其他事情;并单击“我的服务项目”树视图不执行任何操作(它没有子项)。
如果我尝试直接从IE8运行新操作,我得到:
2>'/'应用程序中的服务器错误。在合同'IRestServiceImpl'中,有多个操作使用Method'GET',UriTemplate等同于'xml / {platypusId}'。每个操作都需要UriTemplate和Method的唯一组合来明确地分派消息。使用WebGetAttribute或WebInvokeAttribute更改操作的UriTemplate和Method值。
这是否意味着我只能进行一次带字符串的xml返回操作?另一个/原始方法是...... xml / {id} ...
这是代码,它仍然失败:
[ServiceContract]
public interface IRestServiceImpl
{
[OperationContract(Name="Foo")]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "xml/{id}")]
string XMLData(string id);
[OperationContract(Name="FooBar")]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "xml/{platypusId, anotherId}")]
string FirstTrial(string platypusId, string anotherId);
[OperationContract(Name="FooFooBar")]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "json/{id}")]
string JSONData(string id);
}
//实施(.svc)文件
public class RestServiceImpl : IRestServiceImpl
{
public string XMLData(string id)
{
return "You requested product " + id;
}
public string FirstTrial(string platypusId, string anotherID)
{
return "I reckon so" + platypusId + anotherID;
}
public string JSONData(string id)
{
return "You requested product " + id;
}
}
答案 0 :(得分:2)
你可以有多个接受String并返回XML的方法,但你不能将它们命名为同一个东西,并且它们都是GET方法。怎么知道你打算打电话到哪一个?
答案 1 :(得分:2)
对于任何类型的Web服务,您都不能使用重载方法。如果您指定不同的OperationContract Name E.G。
,则WCF允许此操作[ServiceContract]
interface IService
{
[OperationContract(Name="Foo")]
void Foo();
[OperationContract(Name="Foobar")]
void Foo(string bar);
}
但这基本上是将公共签名更改为方法,即使它在界面中的名称相同,所以我通常不会这样做,因为在创建客户端时可能会更加混乱。
更新:
确保您在web.config中确认autoformateselectiondable为true。
<endpointBehaviors>
<behavior name="web">
<webHttp automaticFormatSelectionEnabled="true"/>
</behavior>
</endpointBehaviors>
" This will automatically set the response format as per request type (JSON/XML)"