【问题标题】:Why does "WCF Test Client" preempt my WCF app, and how can I avoid it?为什么“WCF 测试客户端”会抢占我的 WCF 应用程序,我该如何避免?
【发布时间】:2012-10-17 21:00:26
【问题描述】:

我的简单示例 wcf 服务工作正常,但突然间它开始在“WCF 测试客户端”对话框中提示我输入端点地址。

我不记得更改任何会导致它从按 F5 时弹出浏览器 (IE 8) 到现在显示这个“WCF 测试客户端”的东西。

我不知道在它提供的编辑框中输入什么,所以我尝试了“http://localhost:4841/RestServiceImpl.svc”(http://localhost:4841/RestServiceImpl.svc/xml/123仍然可以在 Visual Studio 外部工作)

它接受(“服务添加成功”显示在对话框的任务栏中),但不执行其他任何操作;并单击“我的服务项目”树视图什么都不做(它没有孩子)。

更新

如果我尝试直接从 IE8 运行新操作,我会得到:

“/”应用程序中的服务器错误。

在合同“IRestServiceImpl”中,有多个使用方法“GET”和等同于“xml/{platypusId}”的 UriTemplate 的操作。每个操作都需要 UriTemplate 和 Method 的唯一组合来明确地发送消息。使用 WebGetAttribute 或 WebInvokeAttribute 更改操作的 UriTemplate 和 Method 值。

这是否意味着我只能有一个带字符串的 xml 返回操作?另一种/原始方法是 ...xml/{id}...

更新 2

这是代码,但仍然失败:

[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;
    }
}

【问题讨论】:

    标签: c# wcf rest


    【解决方案1】:

    对于任何类型的 Web 服务,您都不能有重载的方法。如果您指定不同的 OperationContract Name E.G.,WCF 允许这样做

    [ServiceContract]
    interface IService
    {
        [OperationContract(Name="Foo")]
        void Foo();
    
        [OperationContract(Name="Foobar")]
        void Foo(string bar);
    
    }
    

    但这基本上是将公共签名更改为方法,即使它在接口中命名相同,所以我通常不会这样做,因为在创建客户端时会更加混乱。

    更新:

    确保在 web.config 中将 autoformateselectionenabled 设置为 true。

    <endpointBehaviors>
        <behavior name="web">
            <webHttp automaticFormatSelectionEnabled="true"/>
        </behavior>
    </endpointBehaviors>
    

    " This will automatically set the response format as per request type (JSON/XML)"

    【讨论】:

    • 我添加了那个(它只有 ),但我仍然得到:在合同 'IRestServiceImpl' 中,有多个使用方法 'GET' 的操作和一个等同于 'xml/ 的 UriTemplate {platypusId,anotherId}'。每个操作都需要 UriTemplate 和 Method 的唯一组合来明确地发送消息。使用 WebGetAttribute 或 WebInvokeAttribute 更改操作的 UriTemplate 和 Method 值。
    【解决方案2】:

    您可以拥有多个接受 String 并返回 XML 的方法,但您不能将它们命名为相同的东西并让它们都是 GET 方法。它怎么知道你打算打电话给哪个?

    【讨论】:

    • 一开始我没有给他们起名字,以为“string xmlData...”和“string FirstTrial...”区分了它们,但即使添加了名字也失败了。我添加了上面的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-30
    • 1970-01-01
    • 2012-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多