在WCF项目中使用参考出现问题... [英] Problem using a reference in a WCF project...

查看:72
本文介绍了在WCF项目中使用参考出现问题...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直遇到以下问题:
我有一个WCF项目MyService,它引用了另一个项目MyProject.另一个项目有一个名为MyClass的类,我将其作为参数传递给WCF方法.
现在,当我在一个简单的控制台应用程序中测试WCF服务时,我需要传递MyService.MyClass类型的Object,但是该类不存在!应该是MyProject.MyClass.
我检查了MyClass中的所有内容都是可序列化的,所以应该不成问题.
到目前为止,我唯一要做的就是将MyClass传递给Method,而没有别的.它不会在MyClass中调用任何其他方法.
实际上,在控制台应用程序中,我可以实例化MyProject中实际定义的MyService中的所有内容.
为什么我的WCF服务/控制台应用程序认为所有这些类都属于MyService而不是MyProject?


我在服务端和客户端都引用了MyProject.
更重要的是,这几天前就奏效了.我对MyProject进行了一些更改(不是MyClass或它使用的任何类),现在它不再起作用了.
我的ServiceContract如下所示:

I''ve been having the following problem:
I have a WCF project, MyService, which references another project, MyProject. This other project has a Class called MyClass and I am passing it as a parameter to a WCF Method.
Now when I test my WCF service in a simple Console app I need to pass an Object of the type MyService.MyClass, but this Class is non-existant! It should be MyProject.MyClass.
I have checked that everything in MyClass is Serializable, so that should not be the problem.
So far the only thing I do is pass MyClass to a Method and nothing else. It does not call other Methods in MyClass whatsoever.
And actually, in my Console app I can instantiate all kinds of stuff from MyService that are actually defined in MyProject.
Why does my WCF service/Console app think that all those Classes belong to MyService instead of MyProject?


I have the MyProject referenced in both service and client side.
What''s more, this worked a few days ago. I made some changes to MyProject (not MyClass or any Class it uses though) and now it doesn''t work anymore.
My ServiceContract looks as follows:

<ServiceContract()>
Public Interface IMyService
   <OperationContract()>
   Function DoSomething(mc As MyProject.MyClass) As Integer
End Interface



这是服务.



Here is the Service.

Public Class MyService
   Implements IMyService

   Public Function DoSomething(mc As MyProject.MyClass) As Integer Implements IMyService.DoSomething
      ' DoMoreStuff returns an Integer.
      Return SomethingCompletelyDifferent.DoMoreStuff(mc.DoStuff)
   End Function
End Class



MyClass的定义如下:



MyClass is defined as follows:

<Serializable()>
Public Class MyClass
   ' Lots of Properties, all of which are serializable and
   ' contain nothing more than Strings and Integers and some Enums.
   Public Function DoStuff As AnotherClass
      ' Do stuff with non-serializable Classes and return a non-serializable Class
   End Function
End Class



SomethingCompletelyDifferent和AnotherClass保留在服务内,并且不与客户端应用程序通信.因此,我不希望这些成为问题.我的客户端应用程序引用了MyProject,只需执行以下操作即可.



SomethingCompletelyDifferent and AnotherClass stay within the Service and do not communicate to the client app. So I do not expect those to be the problem. My client app has a reference to MyProject and simply does the following.

Dim service As ServiceReference.MyService
Dim mc As New MyProject.MyClass
service.DoSomething(mc) ' Error here. Cannot convert MyProject.MyClass to MyService.MyClass.


现在,它是从哪里获得MyService.MyClass的?


Now where did it get MyService.MyClass from?

推荐答案

Nearling,

我通常将数据类放在单独的程序集中,然后在客户端和服务实现项目中都引用该程序集.如果没有这种方法,WCF将尝试为您生成客户端数据类,这通常效果很好.但是,DataMembers之外的任何内容都将对客户端不可用.

我经常使用 svcutil [数据合同 [
Hi Nearling,

I usually place data classes in a separate assembly, and then reference that assembly in both the client and service implementation project. Without this approach WCF will attempt to generate client side data classes for you, which usually works quite well. Anything apart from DataMembers will not be available to the client though.

I often use svcutil[^] directly on the assembly implementing the service for client service reference generation, this way I don''t have to actually have the service up and running, and I don''t need to configure mex for metadata exchange.

WCF will try to use the referenced assemblies if at all possible. Unless I need something really efficient I use Data Contracts[^] - You have more control over how WCF serializes your data.

Naerling写道:
Naerling wrote:

现在它是从哪里获得MyService.MyClass的?

Now where did it get MyService.MyClass from?


添加服务引用时,它很可能是由Visual Studio为您生成的.


It was most likely generated for you by Visual Studio when you added the service reference.


  1. CommonTypes.dll-常见数据类型在这里
  2. ServiceImplmentation.dll-引用CommonTypes.dll
  3. serviceClientImplmentation.dll-引用CommonTypes.dll,请记住在引用之前添加程序集服务
  4. serviceRunner.exe-引用CommonTypes.dll和ServiceImplmentation.dll
  5. clientApplication.exe-引用CommonTypes.dll和serviceClientImplmentation.dll

  1. CommonTypes.dll - common data types goes here
  2. ServiceImplmentation.dll - references CommonTypes.dll
  3. serviceClientImplmentation.dll - references CommonTypes.dll, remember to add the assembly before referencing the service
  4. serviceRunner.exe - references CommonTypes.dll and ServiceImplmentation.dll
  5. clientApplication.exe - references CommonTypes.dll and serviceClientImplmentation.dll



我通常在自己的程序集中实现服务引用,因为我通常还会在服务引用周围添加错误处理包装器.

最好的问候
Espen Harlinn



I usually implement the service reference in it''s own assembly, as I usually also add an error handling wrapper around the service reference.

Best regards
Espen Harlinn


这篇关于在WCF项目中使用参考出现问题...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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