使用共享的数据类型为DataContract在WCF [英] Use a shared datatype as DataContract in WCF

查看:130
本文介绍了使用共享的数据类型为DataContract在WCF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我可以看到我要为每一个类型的数据我想通过WCF传递一个特殊的数据类型,所以如果我有像

From what I can see I have to create a "special" datatype for each kind of data I want to transfer over WCF, so if I have a shared class like

public class District
{
    public long Id { get; set; }
    public string Name { get; set; }
}

和我想使用WCF发送区的对象,我要创建一个datacontract,所以我必须创建一个新的WCF类

and I want to send a District object using WCF, I have to create a datacontract and therefore I have to create a new WCF class

[DataContract]
public class WCFDistrict
{
    [DataMember]
    public long Id { get; set; }

    [DataMember]
    public string Name { get; set; }
}



,然后当我在我的实现中使用它在一个WCF服务,我已经从一个对象中的数据解析到其他

And then when I use it in a WCF service in my implementation I have to parse the data from one object to the other

public WCFDistrict GetDistrict(long id)
{
    var district = _districtRepository.GetDistrict(id);
    return new WCFDistrict {Id = district.Id, Name = district.Name};
}

有什么方法只是重用共享类为DataContract,而不用那些属性就可以了?或者我应该创建它们可以共享,所以我可以只投这两者之间的班接口? ?什么的第三个

Is there someway to just reuse the shared class as a DataContract, without having those attributes on it? Or should I create a interface on the classes they can share so I can just cast it between them? Or something third?

推荐答案

首先,你是不是严格要求提供DataContract; WCF将正确地序列普通老式类对象(PO​​CO)只要你是净3.5 SP1或更高版本。

First, you are not strictly required to provide a DataContract; WCF will serialize Plain Old Class Objects (POCO) correctly as long as you are on .Net 3.5 SP1 or later.

其次,你可以共享相同的物理类文件中在服务器和客户端上都项目;我们有几百个类(和代码),直接共享这样的项目,并将其保存在开发和测试的时间和精力了巨大的金额。

Second, you can share the same physical class file in projects on both the server and client side; we have projects that have hundreds of classes (and code) that are directly shared this way and it saves a tremendous amount of time and effort in development and testing.

有一对夫妇得到这个启动和运行(从内存中这样做,所以我可能需要调整的答案)所需的步骤:

There are a couple of steps required to get this up and running (doing this from memory so I may need adjust the answer):

1)在客户端,如果你是使用VB,在相同的默认命名空间要在客户端使用的类创建一个项目(C#的,这并不重要,因为命名空间嵌入类)。

1) In the client side, if you are using VB, create a project in the same default namespace as the classes that you want to use in the client side (for C#, this isn't important since the namespace is embedded in the classes).

2)的类文件添加到项目中的链接,让你拥有类的一个物理拷贝。

2) Add the class files to the project as links so that you have one physical copy of the class.

3)添加<一个HREF =htt​​p://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx> DataContractSerializer的您的WCF配置,如果你还没有一个了。

3) Add a dataContractSerializer to your WCF configuration if you don't have one already.

4)在客户端,在服务上单击鼠标右键,并选择配置服务引用...在出现的对话框中,确保重用类型在所有引用的程序集被选中,该中的所有引用的程序重用类型选择选项。

4) In the client-side, right-click on the service and choose Configure Service Reference... In the resulting dialog, ensure that Reuse types in all referenced assemblies is checked and that the Reuse types in all referenced assemblies option is chosen.

5),最棘手的部分,以得到这个工作是集合。

5) The trickiest part to getting this working is for collections.

A)装饰用的 Col​​lectionDataContract 属性。

b)中添加一个条目此集合到 reference.svcmap Col​​lectionMappings 表。为了找到reference.svcmap,显示项目中的所有文件,然后展开服务。要编辑它,只是在文件上双击。您将添加在这里,你是每个序列特定集合的条目,你需要那些有名单及项目区分LT;>基地和那些有字典<>基地。如果不走这一步,WCF将自动序列化这些类的底层通用的签名,你将失去利用你的类。

b) Add an entry for this collection to the reference.svcmap's CollectionMappings table. To find the reference.svcmap, show all files in the project and then expand the service. To edit it, just double-click on the file. You will add an entry in here for each specific collection that you are serializing and you need to differentiate between those items that have a List<> base and those that have a Dictionary<> base. If you don't take this step, WCF will automatically serialize these classes to the underlying generic signature and you will lose the use of your classes.

在此表中的条目看起来是这样的:

The entries in this table look something like this:

<CollectionMappings>
  <CollectionMapping TypeName="System.Collections.Generic.Dictionary`2" Category="Dictionary" />
  <CollectionMapping TypeName="System.Collections.Generic.List`1" Category="List" />
  <CollectionMapping TypeName="System.Collections.Specialized.StringCollection" Category="List" />
  <CollectionMapping TypeName="My.Namespace.MyDictionaryCollection" Category="Dictionary" />

当您添加这些条目并保存文件时,WCF客户端生成器将重建的参考。 CS或reference.vb文件取决于你所使用的语言。你可以告诉你,如果没有通过查看生成的代码正确配置的参考:如果代码中包含类定义,那么WCF的代码生成器无法映射到你的复制类由于某种原因

When you add these entries and save the file, the WCF client-side generator will rebuild the reference.cs or reference.vb file depending on what language you are using. You can tell if you don't have the references configured correctly by looking at the generated code: if that code contains class definitions, then the WCF code generator was not able to map into your copied classes for some reason.

最后一点:有时WCF代码生成完全不能生成代码,这始终是由于服务(一个问题通常一类是不够的唯一或类型不能够被序列化或那样的原因)。

One final note: sometimes the WCF code generator completely fails to generate the code and this is always due to a problem in the service (usually a class isn't unique enough or a type was not able to be serialized for one reason or another).

为了调试这类问题,最容易做的就是添加WCF诊断日志记录,这将产生一个可以通过一个特殊的工具(忘记它的名字),它允许你钻到错误消息,并发现究竟出了什么问题被打开的文件。这为我们节省了工作数不清的时间。配置此日志记录,以下内容添加到你的web.config中的<的任何地方;结构> 部分:

In order to debug this type of problem, the easiest thing to do is add WCF diagnostic logging, which will generate a file that can be opened by a special tool (forget the name of it) that allows you to drill into the error messages and discover exactly what went wrong. This has saved us untold hours of work. To configure this logging, add the following to your web.config anywhere in the <configuration> section:

  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel"
              switchValue="Information, ActivityTracing"
              propagateActivity="true">
        <listeners>
          <add name="traceListener"
              type="System.Diagnostics.XmlWriterTraceListener"
              initializeData="c:\log\WcfTrace.svclog"  />
        </listeners>
      </source>
    </sources>
  </system.diagnostics>



一旦你添加了这一点,并保存web.config文件,尝试更新客户端的服务引用,您指定该工具将打开日志文件,然后双击。

Once you have added this and saved web.config, attempt to update the service reference in the client, then double-click on the log file you specified and the tool will open.

这篇关于使用共享的数据类型为DataContract在WCF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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