官术网_书友最值得收藏!

Implementing the HelloWorldService service contract

Now that we have defined a service contract interface, we need to implement it. For this purpose, we will re-use the empty class file that Visual Studio created for us earlier and modify it to make it the implementation class of our service.

Before we modify this file, we need to rename it. In the Solution Explorer window, right-click on the Class1.cs file, select Rename from the context menu, and rename it as HelloWorldService.cs. Visual Studio is smart enough to change all the related files that are references to use this new name. You can also select the file and change its name from the Properties window.

Next, perform the following steps to customize the class file:

  1. Open the HelloWorldService.cs file.
  2. Make it implement IHelloWorldService implicitly as follows:
    public class HelloWorldService: IHelloWorldService
  3. Add a GetMessage method to the class. This is an ordinary C# method that returns a string. You can also right-click on the interface link and select Implement Interface to add the skeleton of this method.
    public string GetMessage(string name)
    {
      return "Hello world from " + name + "!";
    }

The final content of the HelloWorldService.cs file should look like the following:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWorldService
{
    public class HelloWorldService: IHelloWorldService
    {
        public string GetMessage(string name)
        {
            return "Hello world from " + name + "!";
        }
    }
}

Now, build the project. If there is no build error, it means that you have successfully created your first WCF service. If you see a compilation error such as 'ServiceModel' does not exist in the namespace 'System', this is because you didn't add the System.ServiceModel namespace reference correctly. Revisit the previous section to add this reference and you are all set.

Next, we will host this WCF service in an environment and create a client application to consume it.

主站蜘蛛池模板: 榕江县| 福鼎市| 宣汉县| 综艺| 高平市| 蓬莱市| 合川市| 麟游县| 马关县| 米泉市| 兴安盟| 沅江市| 桃江县| 井研县| 泌阳县| 梅河口市| 滁州市| 中阳县| 区。| 宜丰县| 萨迦县| 禹州市| 师宗县| 五华县| 龙江县| 红原县| 平阴县| 东台市| 原阳县| 故城县| 固原市| 香格里拉县| 库尔勒市| 正安县| 云安县| 黔东| 延川县| 龙陵县| 文水县| 珲春市| 谷城县|