【问题标题】:How do I create a simple Web Server using WCF without the ServiceHost class?如何在没有 ServiceHost 类的情况下使用 WCF 创建一个简单的 Web 服务器?
【发布时间】:2011-03-10 16:59:54
【问题描述】:

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

【问题讨论】:

  • 不确定 - ServiceHost 负责启动和初始化整个 WCF 运行时服务器端 - 不确定您是否真的想要复制所有代码...... . 有什么意义,真的吗?似乎重新发明了一个相当复杂且非常复杂的运行时系统——你想从中获得什么??
  • 我不认为我想做的事情很复杂,我在这里找到了一个示例 - msdn.microsoft.com/en-us/library/ms789029(VS.90).aspx - 但无法让它工作。我绝不打算在生产中使用它,我只是想了解构成 WCF 的部分。
  • “因为我想知道它是如何工作的”是重新发明轮子的最佳理由。

标签: c# .net wcf wcf-binding channelfactory


【解决方案1】:

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

【讨论】:

  • 这在 LINQPad (Linqpad.net) 中运行良好,但您确实需要以下使用:System.Runtime.Serialization、System.ServiceModel.Channels、System.ServiceModel、System.ServiceModel.Web。很棒的示例代码。
猜你喜欢
  • 1970-01-01
  • 2012-10-22
  • 2012-04-20
  • 1970-01-01
  • 2013-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-28
相关资源
最近更新 更多