您的位置:首页 > 编程语言

Programing WCF Service(WCF编程 第2版 Juval Lowy著 张逸 译)学习笔记(1)

2011-10-13 16:53 323 查看
1. 实例:

//定义一个单独契约,有利于在不同场景下使用契约

[Servicecontract]

interface ImyContract

{

    [Operationcontract]

    string MyMethod();

}

class MyService:IMyContratct

{

    string MyMethod()

    {

        return "Hello WCF";

    }

}

注意:

Page21:  服务类还有一些实现上的约束,我们避免使用带参构造函数,因为WCF只能使用默认构造函数。同样,虽然类可以使用内部(internal)的属性、索引器以及静态成员,但WCF客户端确无法访问它们。

 

2. 契约命名空间与操作别名:

//命名空间

[Servicecontract(Namespace="MyNamespace")]

interface ImyContract

{

    //操作别名

    [Operationcontract(Name="SomeOperation")]

    string MyMethod();

}

 

3. 托管:

WCF服务类不能凭空存在。每个WCF服务都必须托管(host)在Windows进程中,该进程被称为宿主进程(Host Process)。单个宿主进程可以托管多个服务,而相同的服务类型也能够拖过在多个宿主进程中。

  (1) IIS 托管

    只能使用HTTP协议

(2)自托管

      App.Config:

    <system.serviceModel>

        <services>

            <service name="MyNamespace.MyService">

            .....

            </service>

        </services>

    </system.serviceModel>

public static void Main()

{

    ServiceHost host = new ServiceHost(typeof(MyService));

    host.open();

    Application.Run(new MyForm());

    host.close();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐