作为 ASMX 和 WCF 的服务
我有一个现有的 Web 服务 (ASMX),也需要公开为 WCF。 ASMX 必须保留,并且最好在客户端上不进行任何更改。根据 此我的配置如下。服务层是用 CodeSmith 生成的,虽然我没有编写这些服务,但我知道它们很好,因为它们已经在野外使用了很多年。为了保护无辜者,名字已被更改..咧嘴一笑。
在服务层有一个由 CodeSmith 生成的 XXX.YYY.MyService 类,它是双重装饰的
[ServiceContract( Namespace = "http://XXX.YYY" )]
,
[WebService( Namespace = "http://XXX.YYY", Name = "MyService" )]
我还创建了一个空接口 XXX.YYY.IMyService ,它是由MyService
实现。此时我可以毫无问题地使用 ASMX 服务。
现在,我将一个 .svc 文件添加到服务层,其中包含……
<%@ ServiceHost Language="C#" Debug="true" Service="XXX.YYY.MyService" %>
并且我将服务层的 web.config 配置为……
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="MyServiceBehavior" name="XXX.YYY.MyService">
<endpoint binding="basicHttpBinding" contract="XXX.YYY.IMyService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
如果我构建然后尝试在 Visual Studio 2010 中对服务进行服务引用,我看到 MyService
的 .ASMX 和 .SVC 版本。在“添加服务引用”对话框中展开 .svc 分支会导致引用空 XML 文档的错误。
如果我检查事件日志,我会得到......
WebHost 无法处理请求。 发件人信息:System.ServiceModel.ServiceHostingEnvironment+HostingManager/39449526 异常:System.ServiceModel.ServiceActivationException:由于编译期间出现异常,无法激活服务“/System/MyService.svc”。异常消息是:在服务“MyService”实现的合约列表中找不到合约名称“XXX.YYY.IMyService”.. --->
...但是 MyService
被标记为实现 IMyService
...
public partial class MyService : IMyService
我还尝试将服务的合同属性更改为 MyService
而不是界面。这是可行的,但对于客户端代码来说,创建服务实例的任何尝试都会失败,因为它现在是一个接口。
我希望这是有道理的。请随时询问任何额外的问题。我已尝试尽可能详细。
(不涉及 IIS ..这纯粹是在 Visual Studio 2010 中)。
谢谢。
I have an existing web service (ASMX) that needs to be exposed as WCF as well. ASMX must remain and preferably with no change on the client. As per this I have configured as follows. The service layer is generated with CodeSmith and whilst I didn't write these services I know they are fine as they have been used in the wild for many years. The names have been changed to protect the innocent .. grin.
In the service layer there is an XXX.YYY.MyService
class generated by CodeSmith which is double decorated with
[ServiceContract( Namespace = "http://XXX.YYY" )]
and
[WebService( Namespace = "http://XXX.YYY", Name = "MyService" )]
I have also created an empty interface XXX.YYY.IMyService
which is implemented by MyService
. At this point I can consume the ASMX service with no issues.
Now I add a .svc file to the service layer which contains ...
<%@ ServiceHost Language="C#" Debug="true" Service="XXX.YYY.MyService" %>
... and I configure the service layer's web.config with ...
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="MyServiceBehavior" name="XXX.YYY.MyService">
<endpoint binding="basicHttpBinding" contract="XXX.YYY.IMyService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
If I build and then try and make a service reference in Visual Studio 2010 to the service, I see both .ASMX and .SVC versions of MyService
. Expanding the .svc branch in the Add Service Reference dialog results in an error referring to an empty XML document.
If I examine the event log I get ...
WebHost failed to process a request.
Sender Information: System.ServiceModel.ServiceHostingEnvironment+HostingManager/39449526
Exception: System.ServiceModel.ServiceActivationException: The service '/System/MyService.svc' cannot be activated due to an exception during compilation. The exception message is: The contract name 'XXX.YYY.IMyService' could not be found in the list of contracts implemented by the service 'MyService'.. --->
... but MyService
is marked as implementing IMyService
...
public partial class MyService : IMyService
I have also tried changing the contract attribute for the service to MyService
instead of the interface. That works but for the client code breaks as any attempt to create an instance of the service fails as it is now an interface.
I hope that makes sense. Please feel free to ask anything extra. I have tried to be as detailed as possible.
(No IIS involved .. this is purely in Visual Studio 2010).
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码实现了 IMyService,您确定它是 XXX.YYY.IMyService。
Your code implements IMyService are you sure that it is XXX.YYY.IMyService.
我的答案是将 [ServiceContract] 和 [OperationContract] 声明移至接口。这已经解决了我的问题。
HTH。
The answer for me was to move the [ServiceContract] and [OperationContract] declarations to the interface. This has fixed the issue for me.
HTH.