简体   繁体   English

Silverlight WCF RIA服务项目中的代码生成

[英]Code Generation in a Silverlight WCF RIA Services Project

I have a silverlight 4 project that uses wcf ria services to connect to the server. 我有一个Silverlight 4项目,该项目使用wcf ria服务连接到服务器。 I have a public class that I need to use on both the client (silverlight) and server. 我有一个在客户端(silverlight)和服务器上都需要使用的公共类。 What is the best way to accomplish this? 做到这一点的最佳方法是什么?

I have tried using the .shared.cs file convention on the server as well as a linked file (original is on the server). 我尝试在服务器以及链接文件上使用.shared.cs文件约定(原始文件位于服务器上)。 The client thought there was a duplicate class so I changed it to a partial class (I would prefer not to). 客户认为有一个重复的课程,所以我将其更改为局部课程(我不希望这样做)。 Then it complained about duplicate properties so I tried the solution to this stack overflow question . 然后它抱怨属性重复,因此我尝试了解决此堆栈溢出问题的方法 The problem I run into using this solution is not being able to access my enumerations that are in the original class on the server. 我在使用此解决方案时遇到的问题是无法访问服务器上原始类中的枚举。

I also tried a creating a class library project that both of the other projects could reference (silverlight client and server). 我还尝试了创建一个其他两个项目都可以引用的类库项目(silverlight客户端和服务器)。 The problem is finding a type of project to create that both of the projects can reference. 问题在于找到一种可以创建两个项目都可以引用的项目类型。 Silverlight complains about non silverlight projects and vice versa. Silverlight抱怨非Silverlight项目,反之亦然。

I think the best solution here would be to create a new project in a solution (you may call it Helpers, Common, etc) and add a reference in both client and server projects. 我认为最好的解决方案是在解决方案中创建一个新项目(您可以将其称为Helpers,Common等),并在客户端和服务器项目中添加引用。 That way you will use the same class in both of them and if you change the structure of a class it will affect both projects. 这样,您将在两个项目中使用相同的类,并且如果更改类的结构,则将影响两个项目。

I'm not sure about where your problem came from (what do you mean by not being able to access your enumerations ?) however you may find useful to know that .NET supports a feature called assembly sharing that is meant to share some basic type across silverlight and .net. 我不确定您的问题来自哪里(无法访问枚举是什么意思?),但是您可能会发现了解.NET支持称为程序集共享的功能(该功能可以共享某种基本类型)很有用。跨Silverlight和.net。 Just keep in mind that you should reference the Silverlight assembly, not the silverlight project. 请记住,您应该引用Silverlight程序集,而不是Silverlight项目。 If this break your compilation order, add also a "fake" project reference into your csproj file like this: 如果这破坏了您的编译顺序,请在您的csproj文件中添加一个“伪”项目引用,如下所示:

<ProjectReference Include="..\MySilverlightSharedProject.csproj">
  <ReferenceOutputAssembly>false</ReferenceOutputAssembly>
</ProjectReference>

as documented here 作为记录在这里

Have you considered using the Portable Library Extension tools . 您是否考虑过使用可移植库扩展工具

To quote from here , the Portable Class Library project enables you to write and build managed assemblies that work on more than one .NET Framework platform. 引用此处 ,可移植类库项目使您能够编写和构建可在多个.NET Framework平台上工作的托管程序集。

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

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