简体   繁体   中英

Why can't I create a Service Reference in Visual Studio?

I have several WCF services hosted in one console application. All of them are configured in code to use NetTcpBinding with binding.TransferMode = TransferMode.Streamed Message contracts are used to define their operations (see code below for details)

  • RequestMsgContract1,
  • ResponseMsgContract1,
  • ResponseMsgContract2

For some mysterious reason I can not create a service reference for a service that uses ResponseMsgContract1 and ResponceMsgContract2 message contracts simultaneously (see IMyService1 defenition below). The message I get is

The URI prefix is not recognized. Metadata contains a reference that cannot be resolved: 'net.tcp://localhost:8890/MyService1/mex'. Metadata contains a reference that cannot be resolved: 'net.tcp://localhost:8890/MyService1/mex'. If the service is defined in the current solution, try building the solution and adding the service reference again.

Service references for two other services that use only RequestMsgContract1 and ResponseMsgContract2 (see IMyService2) or only RequestMsgContract1, ResponseMsgContract1 (see IMyService3) are created without any problems.

My question is What's wrong with my message contracts or where else should I look to find some clue?

I didn't paste my service configuration code here (as I said I do not use xml config file) because it works Ok for two of three services. I do not think the reason of error is there but you can find full code of service host console application here 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);
}

They use these three message contracts

[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;
        }
    }
} 

EDIT: In case it is important to reproduce the problem here are my Visual Studio, .Net Framework and OS versions

  • Visual Studio 2012 (11.0.61030.00 Update 4)
  • .Net Framework Version 4.5.50709
  • Windows 8 Pro

I do not understand why the error I described in question happens but no one has provided a proper answer yet so I'll tell about workarounds I came up with. Maybe it will help somebody.

I found two ways to make Visual Studio create Service References I need while preserving all the operations I want.

  1. Error disappears if I rename ResponceMsgContract2.Length to ResponceMsgContract2.Length2 so that ResponceMsgContract2 and ResponseMsgContract1 do not have message headers with same name. Why this helps is still a mystery to me. Maybe it's WCF bug.

  2. Error disappears if I split IMyService1 to two interfaces:

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

Both variants are not good solutions and there may be situations when you can't apply any of them. But at least it's something.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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