如何创建主机应用程序以接收数据并发回响应

时间:2015-04-09 18:12:18

标签: c# wcf soap

我是编程的新手,我已经提供了一个项目,我不知道该怎么做。

我必须编写一个主机应用程序。以下是要求:

  

开发CXML webservice主机,用于测试来自xyz.net的CXML帖子。

     

应用程序应读取数据流,并针对dtd进行验证。将其存储到相应的表中并将响应发送回客户端。

1 个答案:

答案 0 :(得分:0)

这应该可以帮助你。创建一个控制台应用程序,并将启动对象设置为SelfHost。运行该应用程序,您应该通过代码中所述的URL提供wcf服务:http://localhost:1234/hello"

[ServiceContract()]
public interface IIndexer
{
    [OperationContract]
    bool Test();

}

public class Indexer : IIndexer
{
    public bool Test()
    {
        return true;
    }
}


    class SelfHost
    {
        static void Main(string[] args)
        {
            Uri baseAddress = new Uri("http://localhost:1234/hello");

            // Create the ServiceHost.
            using (ServiceHost host = new ServiceHost(typeof(Indexer), baseAddress))
            {
                // Enable metadata publishing.
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
                host.Description.Behaviors.Add(smb);

                // Open the ServiceHost to start listening for messages. Since
                // no endpoints are explicitly configured, the runtime will create
                // one endpoint per base address for each service contract implemented
                // by the service.
                host.Open();

                Console.WriteLine("The service is ready at {0}", baseAddress);
                Console.WriteLine("Press Q and <Enter> to stop the service.");
                Console.ReadLine();


                // Close the ServiceHost.
                host.Close();
            }
        }
    }