我如何使用WCF数据服务? [英] How do i Consume WCF Data Service?

查看:121
本文介绍了我如何使用WCF数据服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个WCF服务,但我已经使用3个项目吧;
1)ServiceLibrary(WCF库)
2)网络
3)ConsoleTestClient
我的 ServiceLibrary app.config文件看起来像这样;

i have created a wcf service but i have used 3 projects for it;
1) ServiceLibrary (WCF library)
2) Web
3) ConsoleTestClient
my ServiceLibrary app.config file looks like this;

  <system.serviceModel>
    <services>
      <service name="MrDAStoreJobs.ServiceLibrary.AdvertisementService">
        <clear />
        <endpoint address="basic" 
                  binding="basicHttpBinding" bindingConfiguration="" 
                  contract="MrDAStoreJobs.ServiceLibrary.Interface.IAdvertisementService" />
        <endpoint name="mexHttpBinding"
          contract="IMetadataExchange"
          binding="mexHttpBinding"
          address="mex" />

        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:13758/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, 
          set the value below to false before deployment -->
          <serviceMetadata httpGetEnabled="True" />
          <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel> <br />

现在,举办这个图书馆,我在网​​络我的 Web.config中文件进行以下设置项目。
SVC的文件名是 WcfDataService1.svc

Now, to host this library, i have done the following settings in my Web.Config file of the Web Project.
The svc file name is WcfDataService1.svc

    public class WcfDataService1 : DataService<AdvertisementService>
    {
        // This method is called only once to initialize service-wide policies.
        public static void InitializeService(DataServiceConfiguration config)
        {
            config.UseVerboseErrors = true;
ServiceOperationRights.All);
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
        }
    }
  <system.serviceModel>
    <services>
      <service name="MrDAStoreJobs.ServiceLibrary.AdvertisementService">
        <clear />
        <endpoint address="basic" 
                  binding="basicHttpBinding" bindingConfiguration="" 
                  contract="MrDAStoreJobs.ServiceLibrary.Interface.IAdvertisementService" />
        <endpoint name="mexHttpBinding"
          contract="IMetadataExchange"
          binding="mexHttpBinding"
          address="mex" />

        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:13758/WcfDataService1.svc" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, 
          set the value below to false before deployment -->
          <serviceMetadata httpGetEnabled="True" />
          <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

现在,当我直接使用(ServiceLibrary项目)使用WCF测试客户端测试该服务,我看到下面和工程一切伟大的;
问题是,当我尝试运行我的网​​站的项目(这是我作为一个主机WCF服务中使用)。然后去到控制台测试客户端,并希望通过添加引用添加引用。我没有看到我的获取设置方法(如测试客户端) 为什么我看不到我的IAdvertisementService界面和方法
我必须部署这对实际工作的IIS?

Now, when i test this service using directly (ServiceLibrary project) using WCF test client, i see the following and works everything great;
The issue is when i try to run my Web project(which i use as a host for wcf service). And then go to the console test client and want to add reference using add reference. I don't see my Get and Set methods (like test client) Why i don't see my IAdvertisementService interface and the methods
Do i have to deploy this to actuall IIS?

推荐答案

要开发使用ASP.NET服务,我们必须WebService属性添加到类和WebMethodAttribute的任何一个类的方法。

To develop a service using ASP.NET, we must add the WebService attribute to the class and WebMethodAttribute to any of the class methods.

示例

[WebService] 
 public class Service : System.Web.Services.WebService 
  { 
  [WebMethod] 
  public string Test(string strMsg) 
  { 
      return strMsg; 
  } 
 }

要发展WCF服务,我们将编写以下code:

To develop a service in WCF, we will write the following code:

[ServiceContract] 
public interface ITest 
{ 
   [OperationContract] 
   string ShowMessage(string strMsg); 
 } 


public class Service : ITest 
   { 
       public string ShowMessage(string strMsg) 
       { 
          return strMsg; 
       } 
   }

ServiceContractAttribute的指定接口定义了一个WCF服务合同, OperationContract特性指示哪个的接口的方法定义了服务合同的操作

The ServiceContractAttribute specifies that an interface defines a WCF service contract, OperationContract attribute indicates which of the methods of the interface defines the operations of the service contract.

这是实现服务合同的类被称为在WCF服务类型。

A class that implements the service contract is referred to as a service type in WCF.

承载服务

ASP.NET web服务被编译成一个类库组件和带的.asmx将具有code代表服务的扩展服务文件。服务文件复制到ASP.NET应用程序的根和大会将被复制到bin目录。使用服务文件的URL的应用程序进行访问。

ASP.NET web services are compiled into a class library assembly and a service file with an extension .asmx will have the code for the service. The service file is copied into the root of the ASP.NET application and Assembly will be copied to the bin directory. The application is accessible using URL of the service file.

WCF服务可以IIS或WindowsActivati​​onService内举行。

WCF Service can be hosted within IIS or WindowsActivationService.

编译服务类型到类库 与扩展.SVC到一个虚拟目录,并组装成虚拟目录的bin子目录目录复制服务文件。 web.config文件复制到虚拟目录。

Compile the service type into a class library Copy the service file with an extension .SVC into a virtual directory and assembly into bin sub directory of the virtual directory. Copy the web.config file into the virtual directory.

客户端开发

客户端的ASP.NET Web服务。

Clients for the ASP.NET Web services are generated using the command-line tool WSDL.EXE.

WCF使用的ServiceMetadata工具(svcutil.exe的),以生成客户端的服务

WCF uses the ServiceMetadata tool (svcutil.exe) to generate the client for the service.

有关详细转到此链接 的http://www.$c$cproject.com/Articles/139787/What-s-the-Difference-between-WCF-and-Web-Services

For more detail goto this link http://www.codeproject.com/Articles/139787/What-s-the-Difference-between-WCF-and-Web-Services

这篇关于我如何使用WCF数据服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆