使用Channel Factory无法调用匿名WCF服务

时间:2014-09-26 12:14:42

标签: c# wcf authentication proxy

我正在迁移服务引用以使用频道工厂。

我将接口从服务实现拆分为一个单独的类库。

  • Class Lib:IService
  • Class Lib:Service
  • 网络应用程序:参考IService

代码:

配置

<bindings>
      <basicHttpBinding>
        <binding name="basicHttpBindingEndpoint" maxReceivedMessageSize="5242880"/>
      </basicHttpBinding>
    </bindings>

类别:

public class ProxyManager
{
    internal static ConcurrentDictionary<string, ChannelFactory<IService>> proxies = 
                    new ConcurrentDictionary<string, ChannelFactory<IService>>();

    internal static ChannelFactory<IService> CreateChannelFactory()
    {
        Global.Logger.Info("ProxyManager:CreateChannelFactory");
        BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
        EndpointAddress endpointAddress = new EndpointAddress("http://domain/Service.svc");
        var channelFactory = new ChannelFactory<IService>(basicHttpBinding, endpointAddress);
        return channelFactory;
    }

    internal static IService GetProxy(string key)
    {
         Global.Logger.Info("ProxyManager:GetProxy");
         return proxies.GetOrAdd(key, m => CreateChannelFactory()).CreateChannel();
    }

    internal static bool RemoveProxy(string key)
    {
        Global.Logger.Info("ProxyManager:RemoveProxy");
        ChannelFactory<IService> proxy;
        return proxies.TryRemove(key, out proxy);
    }
}

全局:

public static IService ServiceProxy
{
    get
    {
        return ProxyManager.GetProxy("Service");
    }
}

用法:

ServiceProxy.Method();

错误:

例外:

  

http://domain/Service.svc没有端点收听   可以接受这个消息。这通常是由错误的地址引起的   或SOAP动作。有关更多详细信息,请参阅InnerException(如果存在)。

     

InnerException:远程服务器返回错误:(404)Not Found。

  1. 可以在"http://domain/Service.svc"
  2. 上访问该服务
  3. 该服务有&#34; Anonymous&#34;启用身份验证和&#34; Windows&#34; IIS上的身份验证已禁用
  4. 我在这里缺少什么?

    故障排除: 我尝试将绑定设置为&#34; BasicHttpSecurityMode.None&#34;这没有帮助。 2.尝试将Channel Factory窗口凭据设置为&#34; TokenImpersonationLevel.Anonymous&#34;这没有帮助 3.尝试读取endpointaddress的IsAnonymous属性,它为false

    是否可以将频道或频道工厂设置为匿名? 是否可以将通道或通道工厂设置为具有命名空间?

    详细的错误消息:

    Exception:
    Message: There was no endpoint listening at http://domain/Service.svc that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
    Source: mscorlib
    StackTrace: Server stack trace: 
       at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
       at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
       at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
       at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
       at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
       at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
    
    Exception rethrown at [0]: 
       at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
       at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
       at IService.Method()
    TargetSite: Void HandleReturnMessage(System.Runtime.Remoting.Messaging.IMessage, System.Runtime.Remoting.Messaging.IMessage)
    HelpLink:
    Data: System.Collections.ListDictionaryInternal
    
    InnerException:
    Message: The remote server returned an error: (404) Not Found.
    Source: System
    StackTrace:   at System.Net.HttpWebRequest.GetResponse()
       at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
    TargetSite: System.Net.WebResponse GetResponse()
    HelpLink: 
    Data: System.Collections.ListDictionaryInternal
    

1 个答案:

答案 0 :(得分:0)

服务有两个端点--Ajax和Basic。

我没有提到我想在ProxyManager中使用Basic端点。

一旦我更改了下面的代码段,就可以了:

internal static ChannelFactory<IService> CreateChannelFactory()
    {
        Global.Logger.Info("ProxyManager:CreateChannelFactory");
        BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
        EndpointAddress endpointAddress = new EndpointAddress("http://domain/Service.svc/Basic");
        var channelFactory = new ChannelFactory<IService>(basicHttpBinding, endpointAddress);
        return channelFactory;
    }

错误消息对我来说并不清楚,因为服务地址有效。我应该根据错误消息更正端点地址。