How to call a REST-style service operation for WCF WCF endpoint?

Is it possible to invoke a maintenance operation on the urc wcf endpoint using self-service?

I want to invoke some default operation when a client logs in to the url of this service.

In the following example, these uris correctly call the declared operations (SayHello, SayHi):

- http://localhost:4711/clerk/hello
- http://localhost:4711/clerk/hi

But uri

- http://localhost:4711/clerk

does not invoke the announced SayWelcome operation. Instead, it leads to the well-known page "Refusal to publish metadata." Enabling mex does not help, in this case the mex page is displayed at the end of the uri.

private void StartSampleServiceHost()
{
    ServiceHost serviceHost = new ServiceHost(typeof(Clerk), new Uri( "http://localhost:4711/clerk/"));
    ServiceEndpoint endpoint = serviceHost.AddServiceEndpoint(typeof(IClerk), new WebHttpBinding(), "");
    endpoint.Behaviors.Add(new WebHttpBehavior());
    serviceHost.Open();
}

[ServiceContract]
public interface IClerk
{
    [OperationContract, WebGet(UriTemplate = "")]
    Stream SayWelcome();

    [OperationContract, WebGet(UriTemplate = "/hello/")]
    Stream SayHello();

    [OperationContract, WebGet(UriTemplate = "/hi/")]
    Stream SayHi();
}    

public class Clerk : IClerk
{
    public Stream SayWelcome() { return Say("welcome"); }

    public Stream SayHello() { return Say("hello"); }

    public Stream SayHi() { return Say("hi"); }

    private Stream Say(string what)
    {
        string page = @"<html><body>" + what + "</body></html>";
        return new MemoryStream(Encoding.UTF8.GetBytes(page));
    }
}

Is there a way to disable mex processing and enable the declared operation?

Thanks Dieter

0
source share
1 answer

?

[OperationContract, WebGet(UriTemplate = "/")]
Stream SayWelcome();

UPDATE:

, , WCF :

[ServiceContract]
public interface IDiscoveryService {

    [OperationContract]
    [WebGet(BodyStyle=WebMessageBodyStyle.Bare, UriTemplate="")]
    Stream GetDatasets();

, , , WebServiceHost ServiceHost.

+1

Source: https://habr.com/ru/post/1746118/


All Articles