繁体   English   中英

我如何使用WCF数据服务?

[英]How do i Consume WCF Data Service?

我已经创建了一个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 Project的Web.Config文件中完成了以下设置。
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项目)测试此服务时,我看到以下内容并且一切都很好; 在此输入图像描述
问题是当我尝试运行我的Web项目(我用作wcf服务的主机)。 然后转到控制台测试客户端,并希望使用添加引用添加引用。 我没有看到我的GetSet方法(比如测试客户端) 在此输入图像描述 为什么我没有看到我的IAdvertisementService接口和方法
我是否必须将其部署到执行IIS?

若要使用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

上一篇文章删除:


更新:

Microsoft开发人员网络实际上涵盖了这一点,它们提供的一些资源包括:

还有几本书我解决了这个特别的努力。 由于有人说提供解决此问题的链接并不能真正回答您将要尝试解决的问题。

  1. 在Visual Studio中单击“ 文件” ,然后继续“ 新建项目”
  2. 在对话框中,展开Visual C# ,选择“ Web”和“ ASP.NET Web窗体应用程序”
  3. 为您的项目命名您选择的NorthwindWeb

至此,您已经创建了一个项目。 由于服务的复杂性,忽略微小的细节可能会导致灾难性的后果。 这就是为什么我从头开始。

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

重要的部分,构建我的Data Service

  1. 项目添加新项目
  2. 选择Web并选择WCF数据服务
  3. 放一个名字, NorthwindCustomer - 然后添加

找到第一个Todo:注释并删除代码然后放入:

public class DemonDbCustomer : DataService<demonDbEntities>

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

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

此时按CTRL + F5运行该服务。 浏览器将打开,并且将生成该服务的XML模式。 地址栏中 ,在NorthwindCustomers.svc的URL末尾键入客户 ,然后按Enter

**有时Internet Explorer会将其弄乱,因此可能需要进行其他故障排除。 **

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

  1. 添加 新项目
  2. 选择Windows窗体应用程序
  3. 将文件命名为NorthwindClient然后单击确定
  4. Solution Explorer中,选择NorthwindClient ProjectSet As Startup Project
  5. 右键单击项目: 添加服务引用单击“ 发现”

此时,您NorthwindCustomers Service的URL将显示在该“ 地址”字段中。 这是从.svc文件生成的。

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

  1. 数据菜单上,我们要显示数据源
  2. 添加新数据源
  3. 选择Data Source的类型,然后按照向导(单击对象)。
  4. 选择您想要绑定的对象。

现在,此时您只需要创建一个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模式,因此您只需从地址栏中复制该URL。 然后用您刚复制的Uri替换Uri

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

  • 主办
  • 客户
  • 服务
  • 消费

这就是使用WCF数据服务的方式, 这里有更多详细信息

希望这会有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM