我如何使用WCF数据服务?

时间:2013-03-06 17:40:38

标签: c# asp.net .net wcf wcf-data-services

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

  <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项目的Web文件中完成了以下设置。
svc文件名为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>

现在,当我使用WCF测试客户端直接使用(ServiceLibrary项目)测试此服务时,我看到以下内容并且工作得很好; enter image description here
问题是当我尝试运行我的Web项目(我用作wcf服务的主机)时。然后转到控制台测试客户端,并希望使用添加引用添加引用。我没有看到我的GetSet方法(比如测试客户端) enter image description here 为什么我看不到我的IAdvertisementService界面和方法
我是否必须将其部署到执行IIS?

2 个答案:

答案 0 :(得分:6)

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

实施例

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

要在WCF中开发服务,我们将编写以下代码:

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


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

ServiceContractAttribute指定接口定义WCF服务合同, OperationContract属性指示接口的哪些方法定义服务合同的操作。

实现服务合同的类在WCF中称为服务类型。

托管服务

将ASP.NET Web服务编译为类库程序集,扩展名为.asmx的服务文件将包含该服务的代码。服务文件将复制到ASP.NET应用程序的根目录中,Assembly将被复制到bin目录中。可以使用服务文件的URL访问该应用程序。

WCF服务可以在IIS或WindowsActivationService中托管。

将服务类型编译为类库 将扩展名为.SVC的服务文件复制到虚拟目录中,并汇编到虚拟目录的bin子目录中。 将web.config文件复制到虚拟目录中。

客户开发

使用命令行工具WSDL.EXE生成ASP.NET Web服务的客户端。

WCF使用ServiceMetadata工具(svcutil.exe)为服务生成客户端。

有关详细信息,请转到此链接 http://www.codeproject.com/Articles/139787/What-s-the-Difference-between-WCF-and-Web-Services

答案 1 :(得分:1)

上一篇文章已删除:


<强>更新

Microsoft Developer Network实际上非常详细地介绍了它,它们提供的一些资源是:

我还有几本书可以解决这个问题。由于有人表示提供解决此问题的链接并不能真正回答您的问题,我会尝试。

  1. 在Visual Studio中单击文件,然后继续新建项目
  2. 在对话框中展开 Visual C#,选择 Web ASP.NET Web窗体应用程序
  3. 为您的项目命名NorthwindWeb
  4. 此时你已经创建了一个项目;由于服务的复杂性忽略了一个微小的细节,结果可能是灾难性的。这就是我从头开始的原因。

    在我的示例中,我将其链接到数据库。所以我将添加 Ado.Net实体数据模型。我将我的模型命名为NorthwindModel。我也将基于现有的数据库进行生成。所以在这一点上只需遵循Visual Studio向导。在这些表中选择数据库对象,然后单击完成。

    重要的是,构建我的Data Service

    1. 项目添加新项目
    2. 选择 Web 并选择 WCF数据服务
    3. 输入名称NorthwindCustomer - 然后添加
    4. 找到第一个Todo:评论并删除代码,然后输入:

      public class DemonDbCustomer : DataService<demonDbEntities>
      

      然后在InitializeService事件处理程序中找到注释:

      config.SetEntitySetAccessRule("*", EntitySetRights.All);
      

      此时点击 CTRL + F5 来运行该服务。浏览器将打开,服务的XML Schema将生成。在地址栏中,在NorthwindCustomers.svc的网址末尾的 Customers 中输入 Enter

      **有时Internet Explorer会搞砸它,因此可能需要进行额外的故障排除。 **

      现在我们将创建我们的客户端部分。

      1. 添加 新项目
      2. 选择 Windows窗体应用程序
      3. 将您选择的文件命名为NorthwindClient,然后点击确定
      4. 解决方案资源管理器中,选择NorthwindClient Project设置为启动项目
      5. 右键单击项目:添加服务参考单击发现
      6. 此时,NorthwindCustomers Service的网址将显示在地址字段中。这是从.svc文件生成的。

        现在我们必须为我们的服务提供数据绑定。

        1. 数据菜单上,我们要显示数据源
        2. 添加新数据源
        3. 选择Data Source的类型,然后按照向导(单击对象)。
        4. 选择您要绑定的对象。
        5. 现在,您需要创建一个User Interface。要执行此操作,只需将Customers NodeData Sources拖到Form

          • DataGridView
          • BindingSource
          • BindingNavigation

          它们都是自动添加的。然后,只需双击Form,然后将以下内容添加到Form1_Load Event Handler

          ServiceReference1.northwindModel.northwindEntities proxy = new 
               ServiceReference1.northwindModel.northwindEntities(new
                   Uri("http://localhost:53397/NorthwindCustomers.svc/"));
          
          // As you see it pointed to our SVC file, because that includes our Address, Binding, Contract information.
          
          this.customersBindingSource.DataSource = proxy.Customers;
          

          现在,在解决方案资源管理器中右键单击NorthwindCustomers.svc,然后单击在浏览器中查看。将添加XML Schema,因此您只需从地址栏复制该URL。然后将Uri替换为刚刚复制的{{1}}。

          运行您的应用程序,您已完成以下操作:

          • 主机
          • 客户端
          • 服务
          • 消耗

          这就是如何使用 WCF数据服务更具细节的文章是here

          希望这有帮助。

相关问题