简体   繁体   English

使用WebGet的WCF服务不起作用

[英]WCF service with WebGet doesn't work

I have a WCF service where everything is started programmatically (and need to continue doing so) and I want this service to respond to [WebGet] attributes. 我有一个WCF服务,其中所有内容[WebGet]编程方式启动(并且需要继续这样做),并且我希望该服务响应[WebGet]属性。

However, when I call one of the WCF methods the service returns a 400 Bad Request. 但是,当我调用WCF方法之一时,该服务将返回400 Bad Request。 This would initially seem like a duplicate of WCF Service Returns 400 error when using WebGet or Bad Request 400 while accessing WCF Rest service (WebGet) but both of these solutions add to web.config , a file I don't have because I need to do everything programmatically. 最初看起来像WCF服务的复制品, 当使用WebGet访问WCF Rest服务(WebGet) 时出现 Bad Request 400时 返回400错误,但是这两个解决方案都添加到了web.config ,我没有这个文件,因为我需要以编程方式进行所有操作。

I have tried to add a WebHttpBinding to what seems like the endpoint, but it doesn't work properly (I'm probably not doing it the correct way). 我试图将WebHttpBinding添加到看起来像终结点的位置,但是它不能正常工作(我可能没有以正确的方式进行操作)。

The code below starts without errors, but when I try to go to http://localhost:8765/MyService/MyTest I get the aforementioned 400 Bad Request 下面的代码开始没有错误,但是当我尝试转到http://localhost:8765/MyService/MyTest了上述400 Bad Request

What am I missing? 我想念什么?

WCF starter WCF启动器

MyService myService = new MyService();
ServiceHost host = new ServiceHost(myService, new Uri("http://localhost:8765/MyService"));
ServiceBehaviorAttribute behavior = host.Description.Behaviors.Find<ServiceBehaviorAttribute>();
behavior.InstanceContextMode = InstanceContextMode.Single;

foreach(ServiceEndpoint endpoint in host.Description.Endpoints) {
    endpoint.Binding = new WebHttpBinding();
}

host.Open();

Service interface 服务介面

[ServiceContract]
public interface IMyService {
    [OperationContract]
    [WebGet]
    string MyTest();
}

Service implementation 服务实施

public class MyService : IMyService {
    public string MyTest() {
        return "Response from WCF service";
    }
}

I use this code I wrote to initialize and start my WCF restful services completely from code: 我使用编写的以下代码完全从代码初始化和启动WCF Restful服务:

public static WebServiceHost InitializeAndStartWebServiceHost(int port, string endPointName, object serviceModel, Type implementedContractType) {
        var baseAddress = new Uri($"http://0.0.0.0:{port}/{endPointName}");
        WebServiceHost host;
        try {
            host = new WebServiceHost(serviceModel, baseAddress);
        } catch (Exception exception) {
            Debug.Print("Error when creating WebServiceHost, message: " + exception.Message);
            return null;
        }

        // ReSharper disable once UseObjectOrCollectionInitializer
        var binding = new WebHttpBinding();
        binding.UseDefaultWebProxy = false;
        binding.BypassProxyOnLocal = true;

        //By default, TransferMode is Buffered which causes C# wcf client to be slow as hell (>500ms for requests which give >2kB responses).
        //I am not exactly sure why this helps, but it helps!
        binding.TransferMode = TransferMode.Streamed;

        host.AddServiceEndpoint(implementedContractType, binding, "");
        var behavior = new WebHttpBehavior();
        behavior.HelpEnabled = false;
        behavior.DefaultBodyStyle = WebMessageBodyStyle.Bare;
        // We will use json format for all our messages.
        behavior.DefaultOutgoingRequestFormat = WebMessageFormat.Json;
        behavior.DefaultOutgoingResponseFormat = WebMessageFormat.Json;
        behavior.AutomaticFormatSelectionEnabled = false;
        behavior.FaultExceptionEnabled = true;


        host.Description.Endpoints[0].Behaviors.Add(behavior);

        try {
            host.Open();
        } catch (AddressAccessDeniedException) {
            Console.WriteLine(@"Application must run with administrator rights.");
            Console.ReadKey();
            Environment.Exit(0);
        }
        return host;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM