为什么“WCF 测试客户端"抢占我的 WCF 应用程序,我该如何避免? [英] Why does "WCF Test Client" preempt my WCF app, and how can I avoid it?

查看:40
本文介绍了为什么“WCF 测试客户端"抢占我的 WCF 应用程序,我该如何避免?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My simple sample wcf service WAS working, but then all of a sudden it started prompting me for an endpoint address in a "WCF Test Client" dialog.

我想不起来有什么改变会导致它从按 F5 时弹出浏览器 (IE 8) 到现在显示这个WCF 测试客户端"的东西.

I can't recall changing anything that would cause it to go from popping up the browser (IE 8) when I hit F5 to now displaying this "WCF Test client" thing.

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

I didn't know what to enter into the edit box it provided, so I tried "http://localhost:4841/RestServiceImpl.svc" (http://localhost:4841/RestServiceImpl.svc/xml/123 still works from outside of Visual Studio)

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

It accepted that ("Service added successfully" displays in the dialog's task bar), but does nothing else; and clicking the "My Service Projects" Treeview does nothing (it has no children).

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

If I attempt to run the new operation directly from IE8, I get:

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

In contract 'IRestServiceImpl', there are multiple operations with Method 'GET' and a UriTemplate that is equivalent to 'xml/{platypusId}'. Each operation requires a unique combination of UriTemplate and Method to unambiguously dispatch messages. Use WebGetAttribute or WebInvokeAttribute to alter the UriTemplate and Method values of an operation.

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

Does this mean I can only have one xml-returning operation that takes a string? The other/original method is ...xml/{id}...

这是代码,它仍然失败:

This is the code, and it's still failing:

[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)文件

// Implementation (.svc) file

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

推荐答案

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

For any type of web service, you cannot have overloaded methods. WCF allows this if you specify a different OperationContract Name E.G.

[ServiceContract]
interface IService
{
    [OperationContract(Name="Foo")]
    void Foo();

    [OperationContract(Name="Foobar")]
    void Foo(string bar);

}

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

But this is basically changing the public signature to the method, even though it is named the same in the interface, so I would generally just not do this, since it can be more confusing when creating your clients.

更新:

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

Make sure that you have autoformateselectionenabled to true in your web.config.

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

" 这将自动根据请求类型(JSON/XML)设置响应格式"

这篇关于为什么“WCF 测试客户端"抢占我的 WCF 应用程序,我该如何避免?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆