【问题标题】:Why can't I create a Service Reference in Visual Studio?为什么我不能在 Visual Studio 中创建服务引用?
【发布时间】:2014-04-21 07:31:19
【问题描述】:

我在一个控制台应用程序中托管了多个 WCF 服务。它们都在代码中配置为使用带有 binding.TransferMode = TransferMode.Streamed 的 NetTcpBinding 消息合约用于定义它们的操作(详见下方代码)

  • RequestMsgContract1,
  • ResponseMsgContract1,
  • ResponseMsgContract2

由于某种神秘的原因,我无法为使用的服务创建服务引用 ResponseMsgContract1 和 ResponceMsgContract2 消息同时收缩(参见下面的 IMyService1 定义)。我得到的消息是

URI 前缀无法识别。 元数据包含无法解析的引用:“net.tcp://localhost:8890/MyService1/mex”。 元数据包含无法解析的引用:“net.tcp://localhost:8890/MyService1/mex”。 如果在当前解决方案中定义了服务,请尝试构建解决方案并再次添加服务引用。

创建仅使用 RequestMsgContract1 和 ResponseMsgContract2(请参阅 IMyService2)或仅使用 RequestMsgContract1、ResponseMsgContract1(请参阅 IMyService3)的其他两个服务的服务引用没有任何问题。

我的问题是我的消息合同出了什么问题,或者我应该在哪里寻找一些线索?

我没有在此处粘贴我的服务配置代码(正如我所说我不使用 xml 配置文件),因为它适用于三个服务中的两个。我认为错误的原因不存在,但您可以在这里找到服务主机控制台应用程序的完整代码http://pastebin.com/1THhc9mU

// Can't create service reference for this service    
[ServiceContract]
interface IMyService1
{
    [OperationContract]
    ResponseMsgContract1 Operation1(RequestMsgContract1 arguments);

    [OperationContract]
    ResponceMsgContract2 Operation2();
}

// No problems with service reference creation for this one
[ServiceContract]
interface IMyService2
{
    [OperationContract]
    ResponceMsgContract2 Operation1();

    [OperationContract]
    ResponceMsgContract2 Operation2(RequestMsgContract1 arguments);
}

// No problems with service reference creation for this one
[ServiceContract]
interface IMyService3
{
    [OperationContract]
    ResponseMsgContract1 Operation1();

    [OperationContract]
    ResponseMsgContract1 Operation2(RequestMsgContract1 arguments);
}

他们使用这三个消息合约

[Serializable]
[MessageContract]
public class RequestMsgContract1
{
    [MessageHeader(MustUnderstand = true)]
    public Guid arg1;
}

[Serializable]
[MessageContract]
public class ResponseMsgContract1 : IDisposable
{
    [MessageHeader(MustUnderstand = true)]
    public long Length;

    [MessageBodyMember(Order = 1)]
    public System.IO.Stream stream;

    public void Dispose()
    {
        if (stream != null)
        {
            stream.Close();
            stream = null;
        }
    }
}

[Serializable]
[MessageContract]
public class ResponceMsgContract2 : IDisposable
{
    [MessageHeader(MustUnderstand = true)]
    public int Length { get; set; }

    [MessageHeader(MustUnderstand = true)]
    public string Str1 { get; set; }

    [MessageBodyMember(Order = 1)]
    public System.IO.Stream stream { get; set; }


    public void Dispose()
    {
        if (stream != null)
        {
            stream.Close();
            stream = null;
        }
    }
} 

编辑: 如果在此处重现问题很重要,请使用我的 Visual Studio、.Net Framework 和 OS 版本

  • Visual Studio 2012(11.0.61030.00 更新 4)
  • .Net 框架版本 4.5.50709
  • Windows 8 专业版

【问题讨论】:

  • 我刚刚发现如果我将ResponceMsgContract2.Length 重命名为ResponceMsgContract2.Length2,创建服务引用的问题就会消失。但我还是不明白原因。
  • 我在 MSDN 论坛上问过同样的问题。 link

标签: c# .net wcf visual-studio visual-studio-2012


【解决方案1】:

我不明白为什么会发生我所描述的错误,但是 还没有人提供正确的答案,所以我将讲述我想出的解决方法。也许它会帮助某人。

我找到了两种方法可以让 Visual Studio 创建我需要的服务引用,同时保留我想要的所有操作。

  1. 如果我将 ResponceMsgContract2.Length 重命名为 ResponceMsgContract2.Length2 以便 ResponceMsgContract2ResponseMsgContract1 没有同名的邮件头。 为什么这有帮助对我来说仍然是一个谜。可能是 WCF 错误。

  2. 如果我将 IMyService1 拆分为两个接口,错误就会消失:

    [ServiceContract]
    interface IMyService1_1
    {
       [OperationContract]
       ResponseMsgContract1 Operation1(RequestMsgContract1 arguments);
    }
    
    [ServiceContract]
    interface IMyService1_2
    {
       [OperationContract]
       ResponceMsgContract2 Operation2();
    }
    

这两种变体都不是好的解决方案,在某些情况下您可能无法应用其中任何一种。但至少它是一些东西。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-04
    • 1970-01-01
    • 2014-08-29
    相关资源
    最近更新 更多