如何在没有 ServiceHost 类的情况下使用 WCF 创建简单的 Web 服务器?

发布于 2024-09-08 01:11:34 字数 150 浏览 7 评论 0原文

我已经开始学习 WCF,并希望通过直接使用通道堆栈创建一个简单的 Web 服务器来了解其内部结构。我在网上找到了很多理论,但我希望看到一个接收和响应 httprequest 的工作示例代码,我可以使用任何浏览器进行测试。 我希望能够通过组装绑定元素来响应请求来显示自定义绑定的设置。

I've began to learn WCF and wish to understand its internals by creating a simple Web server using channel stacks directly. I have found a lot of theory in the web but I'd like to see a working sample code of receiving and responding an httprequest that I can test using any browser.
I'm hoping for something that shows the setup of a custom binding by assembling binding elements all the way to responding the request.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水溶 2024-09-15 01:11:34

在 MSDN 论坛中得到答案:(

static void RunService()
    {
      //Step1: Create a custom binding with just TCP.
      BindingElement[] bindingElements = new BindingElement[2];
      bindingElements[0] = new WebMessageEncodingBindingElement();
      bindingElements[1] = new HttpTransportBindingElement();

      CustomBinding binding = new CustomBinding(bindingElements);


      //Step2: Use the binding to build the channel listener.     
      IChannelListener<IReplyChannel> listener =
         binding.BuildChannelListener<IReplyChannel>(new Uri("http://localhost:8080/channelapp"),
          new BindingParameterCollection());

      //Step3: Listening for messages.
      listener.Open();
      Console.WriteLine(
          "Listening for incoming channel connections");

      //Wait for and accept incoming connections.
      IReplyChannel channel = listener.AcceptChannel();
      Console.WriteLine("Channel accepted. Listening for messages");

      //Open the accepted channel.
      channel.Open();

      //Wait for and receive a message from the channel.
      RequestContext request= channel.ReceiveRequest();

      //Step4: Reading the request message.
      Message message = request.RequestMessage;
      Console.WriteLine("Message received");
      Console.WriteLine("To: {0}", message.Headers.To); // TO contains URL from the browser including query string  
      if (!message.IsEmpty) // HTTP GET does not contain body
      {
        string data = message.GetBody<string>();
        Console.WriteLine("Message content: {0}", data);
      }

      //Send a reply - You can control reply content based on message.Header.To or by message content
      Message replymessage = Message.CreateMessage(binding.MessageVersion, 
        "http://contoso.com/someotheraction", XElement.Parse("<html><body><h1>Hello</h1></body></html>"));

      // Set reply content type
      HttpResponseMessageProperty property = new HttpResponseMessageProperty();
      property.Headers[System.Net.HttpResponseHeader.ContentType] = "text/html; charset=utf-8";
      replymessage.Properties[HttpResponseMessageProperty.Name] = property;

      request.Reply(replymessage);

      //Step5: Closing objects.
      //Do not forget to close the message.
      message.Close();
      //Do not forget to close RequestContext.
      request.Close();
      //Do not forget to close channels.
      channel.Close();
      //Do not forget to close listeners.
      listener.Close();
    }

http://social.msdn.microsoft.com/Forums/en/wcf/thread/09b620e0-ea81-4a6c-8a10-02a032ccd821

Got the answer in MSDN forums :

static void RunService()
    {
      //Step1: Create a custom binding with just TCP.
      BindingElement[] bindingElements = new BindingElement[2];
      bindingElements[0] = new WebMessageEncodingBindingElement();
      bindingElements[1] = new HttpTransportBindingElement();

      CustomBinding binding = new CustomBinding(bindingElements);


      //Step2: Use the binding to build the channel listener.     
      IChannelListener<IReplyChannel> listener =
         binding.BuildChannelListener<IReplyChannel>(new Uri("http://localhost:8080/channelapp"),
          new BindingParameterCollection());

      //Step3: Listening for messages.
      listener.Open();
      Console.WriteLine(
          "Listening for incoming channel connections");

      //Wait for and accept incoming connections.
      IReplyChannel channel = listener.AcceptChannel();
      Console.WriteLine("Channel accepted. Listening for messages");

      //Open the accepted channel.
      channel.Open();

      //Wait for and receive a message from the channel.
      RequestContext request= channel.ReceiveRequest();

      //Step4: Reading the request message.
      Message message = request.RequestMessage;
      Console.WriteLine("Message received");
      Console.WriteLine("To: {0}", message.Headers.To); // TO contains URL from the browser including query string  
      if (!message.IsEmpty) // HTTP GET does not contain body
      {
        string data = message.GetBody<string>();
        Console.WriteLine("Message content: {0}", data);
      }

      //Send a reply - You can control reply content based on message.Header.To or by message content
      Message replymessage = Message.CreateMessage(binding.MessageVersion, 
        "http://contoso.com/someotheraction", XElement.Parse("<html><body><h1>Hello</h1></body></html>"));

      // Set reply content type
      HttpResponseMessageProperty property = new HttpResponseMessageProperty();
      property.Headers[System.Net.HttpResponseHeader.ContentType] = "text/html; charset=utf-8";
      replymessage.Properties[HttpResponseMessageProperty.Name] = property;

      request.Reply(replymessage);

      //Step5: Closing objects.
      //Do not forget to close the message.
      message.Close();
      //Do not forget to close RequestContext.
      request.Close();
      //Do not forget to close channels.
      channel.Close();
      //Do not forget to close listeners.
      listener.Close();
    }

(http://social.msdn.microsoft.com/Forums/en/wcf/thread/09b620e0-ea81-4a6c-8a10-02a032ccd821)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文