无法使用通道工厂调用匿名WCF服务

本文关键字:WCF 服务 调用 工厂 通道 | 更新日期: 2023-09-27 18:24:34

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

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

  • 类库:IService
  • 类库:Service
  • Web应用程序:参考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)找不到。

  1. "http://domain/Service.svc"上可以访问该服务
  2. 该服务在IIS上启用了"匿名"身份验证,并禁用了"Windows"身份验证

我在这里错过了什么?

故障排除:1.我尝试将绑定设置为"BasicHttpSecurityMode.None",但没有帮助。2.尝试将Channel Factory窗口凭据设置为"TokenImpersonationLevel.Anonymous",但没有帮助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

无法使用通道工厂调用匿名WCF服务

Service有两个端点——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;
    }

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