zoukankan      html  css  js  c++  java
  • WCF Part 3 : Hosting the service

    Finally .NET Framework 3.0 RTM'd, so I don't have to run in my VMWare anymore. A good time to see if everything works and create the host for the service contract we've created last time. We'll do this using a C# console application, as those just rock in simplicity.

    We need a ServiceHost object to host our service. It implements IDisposable, so we'll use the using statement to create one.

    using (ServiceHost host = new ServiceHost(xxx))
    {
    }

    You'll hopefully immediately notice the three red x characters. That's where we have to insert a parameter, our servicetype. For us this is our Hello contract. We also need to open the host we created, before it's actually brought alive by WCF.

    Type type = typeof(Hello);//Can we use the IHello? anyone kindly tell me why ? thanks a lot.
     
    using (ServiceHost host = new ServiceHost(type))
    {
      host.Open();
     
      Console.WriteLine("The service is available. Press any key to continue...");
      Console.ReadKey();
     
      host.Close();
    }

    In the above final sample I first declare a type variable, assign our Hello service contract type to it and pass it into the constructor of our ServiceHost instance. I then open the service, show this in the console window and ask the user to press a key to close the service. Else the service would close immediately.

    WCF however needs some information from configuration. It can all be done through code, but of course we like our app.config much better. We need to specify our WCF ABC. We've already created our contract, but we have to let WCF know what contract belongs to what binding on which address. First, compile your application. Then, in your project you can add the app.config, right-click it and select 'Edit WCF Configuration...' and you'll see the Service Configuration Editor. Select 'Create a New Service..." and you'll be presented with a nice wizard.

    The first step we need to tell it what service we'll want to use, where the implementation of the service is. Select to browse and lookup your console application it's executable and select it. You'll see your Hello class, which you'll have to select. The next page of the wizard will now just know that IHello is the contract we've used. Now we'll have to select the first part of our binding, the protocol we'll start using. Leave it at default. In the next step we'll also choose the default setting. The difference between these two can roughly be described as standard ASP.NET ASMX webservices, where the second means WSE 3.0 extension.

    Last thing of the WCF ABC is to select the address. You might want to choose http://localhost:8080/HelloService/. We'll then get an overview of our selections and you can press the finish button. Be sure to check out what the wizard has changed inside the Service Configuration Editor. You'll only have to research the "Services" folder as we did not yet change the others. After saving and looking at our app.config, we should find the following.

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
     
        <system.serviceModel>
            <services>
                <service name="Classa.Wcf.Samples.Hello">
                    <endpoint
                      address="http://localhost:8080/HelloService/"
                      binding="basicHttpBinding"                  
                      contract="Classa.Wcf.Samples.IHello"
                      bindingConfiguration="" />
                </service>
            </services>
        </system.serviceModel>
    </configuration>

    Notice that we have one service. In the above configuration you can see my namespace Classa.Wcf.Samples. which I added extra. In the attachment I've included you can see where exactly this comes from. Also the bindingConfiguration attribute isn't really necessary right now. What is important that we see our WCF ABC return again. Address, binding and contract!

    You can run this and your service will run and be live, but noone can tell how to communicate with it. For this, we need metadata! But that's for the next post.

  • 相关阅读:
    hdu1072 逃离迷宫系列 bfs
    hdu1495 倒水bfs
    hdu 1548 A strange lift (bfs)
    hdu1728 逃离迷宫bfs
    hdu1548 奇怪的电梯 dfs dijkstra bfs都可以,在此奉上dfs
    delphi 窗体的位置和高宽度-TForm:Letf、Top、Width、Height、ClientWidth、ClientHeight
    Delphi 鼠标控制函数GetCursorPos、SetCursorPos
    Delphi CoCreateGuid()函数 获取GUID
    Delphi出现“borland license information was found,but it is not valid for delphi”的错误,无法运行的解决方法
    一维条形码生成与识别技术
  • 原文地址:https://www.cnblogs.com/malaikuangren/p/2552053.html
Copyright © 2011-2022 走看看