Why does “WCF Test Client” preempt my WCF app, and

2019-08-23 12:31发布

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.

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.

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).

UPDATE

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

Server Error in '/' Application.

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.

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

UPDATE 2

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

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

标签: c# wcf rest
2条回答
淡お忘
2楼-- · 2019-08-23 13:01

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.

Updated:

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

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

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

查看更多
forever°为你锁心
3楼-- · 2019-08-23 13:02

You can have more than one method that takes a String and returns XML, but you can't name them the same thing and have them both be GET methods. How would it know which one you are intending to call?

查看更多
登录 后发表回答