我在我的C++/CLI项目(VS2012 Express)中引用System::ServiceModel。下面的代码失败了,出现了以下错误,我无法找到如何修复它。
错误C2337:'ServiceMetadataBehavior‘:属性未找到
[System::ServiceModel::ServiceContractAttribute]
[System::ServiceModel::Description::ServiceMetadataBehavior]
public ref class PlaybackManager
{
public:
~PlaybackManager() { this->!PlaybackManager(); }
!PlaybackManager() { }
// Playback action methods
[System::ServiceModel::OperationContractAttribute]
void Play();
[System::ServiceModel::OperationContractAttribute]
void Stop();
[System::ServiceModel::OperationContractAttribute]
void Pause();
[System::ServiceModel::OperationContractAttribute]
void Previous();
[System::ServiceModel::OperationContractAttribute]
void Next();
[System::ServiceModel::OperationContractAttribute]
void Random();
};EDIT1:
但要注意的是,不可能完全用代码编写wcf服务,即没有app.config文件。虽然服务有创建元数据交换行为实现的ServiceMetadataBehavior助手,但是对于端点没有这样的东西。这是“按设计”吗?如何:使用代码为服务发布元数据
EDIT2:
好的,所以上面的警告似乎不一定是正确的。下面是表示我试图在代码中做什么的app.config,如果我将ServiceMetatdataBehavior属性移除到端点类实现中,我会得到相同的错误。
<configuration>
<system.serviceModel>
<services>
<service name="Engine.PlaybackManager">
<endpoint
address="net.tcp://localhost:7008/PlaybackManager"
binding="mexTcpBinding"
contract="IMetadataExchange"
/>
<endpoint
address="net.tcp://localhost:7008/PlaybackManager"
binding="netTcpBinding"
contract="Engine.PlaybackManager"
/>
</service>
</services>
</system.serviceModel>
</configuration>错误是:
在服务IMetadataExchange实现的合同列表中找不到合同名称“PlaybackManager”。将一个ServiceMetadataBehavior添加到配置文件或直接添加到ServiceHost中,以启用对此契约的支持。
问题是,如果我将ServiceMetadataBehavior属性添加到PlaybackManager类中,就会得到上面的原始错误,它是不被识别的。有什么想法吗?
发布于 2013-04-11 04:58:48
我理解为什么没有人对此作出回应,“我从哪里开始”是唯一可能的反应。因此,如果有人在这件事上犯了和我一样多的困惑,下面是一些建议:
我的主要问题是如何将xml配置命名(在大多数在线示例中)与代码等价物进行映射:
<services> maps to System::ServiceModel::ServiceHost
<behaviors> maps to "your instance of ServiceHost"->Description->Behaviors
<behavior> is type specific, the type being a nested element in the xml, thus:
<behavior> <serviceMetadata /> </behavior> maps to ServiceMetadataBehavior
<endpoint> maps to ServiceEndpoint最后: mex端点(添加了ServiceMetadataBehavior )需要它自己的命名空间,所以在实现端点uri地址的末尾添加"/mex“。
example:
implementation address = "net.tcp://localhost:5000/Engine"
mex address = "net.tcp://localhost:5000/Engine/mex"显然,这些提示不是一个解释,但我希望它们能帮助像我问这个问题时一样困惑的人。
https://stackoverflow.com/questions/15593404
复制相似问题