zoukankan      html  css  js  c++  java
  • 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.

     

  • 相关阅读:
    [BZOJ 4350]括号序列再战猪猪侠 题解(区间DP)
    Welcome to Anvet Blogs
    法语笔记
    min-max容斥略解
    求和与和式界的估计
    浅谈虚树
    浅谈随机数的生成
    莫比乌斯反演与狄利克雷卷积
    傅里叶变换三部曲(二)·傅里叶变换的定义
    傅里叶变换三部曲(一)·傅里叶级数
  • 原文地址:https://www.cnblogs.com/malaikuangren/p/2560644.html
Copyright © 2011-2022 走看看