A Test WCF Service without anything of config.

Firstly .  I am a fresh man of WCF . Maybe something i done looks like easy for you .but if you have any comments for me . i will be appreciated for that . thank u .

Recently when i was writing something simple enough WCF server testing program.

in my console app. firstly i had to add system.ServiceModel.dll of .net 4.0 in my computer system like all the others wcf app.

and i add code shown blow in the main method.

static void Main(string[] args)
        {
            ServiceHost host = new ServiceHost(typeof(TestServiceImplement),
                new Uri("http://localhost:8010/TestServices"));
            try
            {
                // Open the service host to accept incoming calls
                host.Open();

                // The service can now be accessed.
                Console.WriteLine("The service is ready.");
                Console.WriteLine("Press <ENTER> to terminate service.");
                Console.WriteLine();
                Console.ReadLine();

                // Close the ServiceHostBase to shutdown the service.
                host.Close();
            }
            catch (CommunicationException commProblem)
            {
                Console.WriteLine("There was a communication problem. " + commProblem.Message);
                Console.Read();
            }

see ? i did not add anything of configurations . let 's what would be happen.

... 

the console wrote the message like this :

"The service is ready." 

"Press <ENTER> to terminate service." 

well . it seems this kind of WCF service without any configurations works .

but . if you access the base uri defined in the main in web browser .

the wcf will show you a message that  inform you can not get the metadata from the service you consumed.

 

so. what are u going to do with a WCF service without metadata ? that is the problem i really want to mention. we knew even the simplest app i wrote before is working without any exception .without metadata. we don't know the detail of how to communicate message to message between the client and server.

so . we had better follow the instructions shown in the above . add service behaviors . add mex endpoint....something like that . that will make sense of the service you created.

then . i add all the stuff that need to be in app.config. also you can done it in code

<?xml version="1.0" encoding="utf-8" ?>
  <configuration>
    <system.serviceModel>
      <services>
        <!-- 注意: 服务名称必须与服务实现的配置名称相匹配。 如果name配置错误那么也会引起无法获取元数据,当然服务还是能启动-->
        <service name="testWCFInConfig1.TestServiceImplement" behaviorConfiguration="MyServiceTypeBehaviors" >
          <!-- 添加下列终结点。 -->
          <!-- 注意: 服务必须有一个 http 基址以便添加此终结点。contract必须是和实际的一致,否则会启动失败,报找不到"xxx"contract -->
          <endpoint contract="testWCFInConfig1.ITestService" binding="wsHttpBinding" address="Test" />
          <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
        </service>
      </services>
      <behaviors>
        <serviceBehaviors>
          <behavior name="MyServiceTypeBehaviors" >
            <!-- 将下列元素添加到服务行为配置中。 -->
            <serviceMetadata httpGetEnabled="true" />
          </behavior>
        </serviceBehaviors>
      </behaviors>
    </system.serviceModel>
</configuration> 

after configuration. access the WCF in browser again . everything is fine now.

 

原文地址:https://www.cnblogs.com/malaikuangren/p/2560644.html