6

我正在尝试跟随MSDN上 WCF 页面的初学者的演示视频。

第一个视频或多或少地工作得很好。我现在接近第二个视频的结尾。我使用的是 VS2010 / .NET 4.0,而视频似乎使用的是 VS2008(我假设是 .NET 3.5,但我不记得了)。

我正在使用以下演示

我们刚刚添加了另外 3 个端点:一个普通的 http、net.tcp 和 net.pipe。当我现在尝试运行该项目时,Web 服务无法启动。

System.InvalidOperationException: Cannot load the X.509 certificate identity specified in the configuration.
   at System.ServiceModel.Description.ConfigLoader.LoadIdentity(IdentityElement element)
   at System.ServiceModel.Description.ConfigLoader.LoadServiceDescription(ServiceHostBase host, ServiceDescription description, ServiceElement serviceElement, Action`1 addBaseAddress)
   at System.ServiceModel.ServiceHostBase.LoadConfigurationSectionInternal(ConfigLoader configLoader, ServiceDescription description, ServiceElement serviceSection)
   at System.ServiceModel.ServiceHostBase.LoadConfigurationSectionInternal(ConfigLoader configLoader, ServiceDescription description, String configurationName)
   at System.ServiceModel.ServiceHostBase.ApplyConfiguration()
   at System.ServiceModel.ServiceHostBase.InitializeDescription(UriSchemeKeyedCollection baseAddresses)
   at System.ServiceModel.ServiceHost.InitializeDescription(Type serviceType, UriSchemeKeyedCollection baseAddresses)
   at System.ServiceModel.ServiceHost..ctor(Type serviceType, Uri[] baseAddresses)
   at Microsoft.Tools.SvcHost.ServiceHostHelper.CreateServiceHost(Type type, ServiceKind kind)
   at Microsoft.Tools.SvcHost.ServiceHostHelper.OpenService(ServiceInfo info)

根据我的 Google fu,我遇到了这个线程:“无法加载配置中指定的 X.509 证书身份”

我真的不想参与证书,因为我仍然只是在尝试基础知识,所以我按照那篇文章中的建议并添加了<dns value="localhost" />标签。异常变化:

Please try changing the HTTP port to 8732 or running as Administrator.
System.ServiceModel.AddressAccessDeniedException: HTTP could not register URL http://+:8080/EvalService/. Your process does not have access rights to this namespace (see http://go.microsoft.com/fwlink/?LinkId=70353 for details). ---> System.Net.HttpListenerException: Access is denied
   at System.Net.HttpListener.AddAllPrefixes()
   at System.Net.HttpListener.Start()
   at System.ServiceModel.Channels.SharedHttpTransportManager.OnOpen()
   --- End of inner exception stack trace ---
   at System.ServiceModel.Channels.SharedHttpTransportManager.OnOpen()
   at System.ServiceModel.Channels.TransportManager.Open(TransportChannelListener channelListener)
   at System.ServiceModel.Channels.TransportManagerContainer.Open(SelectTransportManagersCallback selectTransportManagerCallback)
   at System.ServiceModel.Channels.TransportChannelListener.OnOpen(TimeSpan timeout)
   at System.ServiceModel.Channels.HttpChannelListener.OnOpen(TimeSpan timeout)
   at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
   at System.ServiceModel.Channels.DatagramChannelDemuxer`2.OnOuterListenerOpen(ChannelDemuxerFilter filter, IChannelListener listener, TimeSpan timeout)
   at System.ServiceModel.Channels.SingletonChannelListener`3.OnOpen(TimeSpan timeout)
   at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
   at System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen(TimeSpan timeout)
   at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
   at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout)
   at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
   at System.ServiceModel.Security.NegotiationTokenAuthenticator`1.OnOpen(TimeSpan timeout)
   at System.ServiceModel.Security.WrapperSecurityCommunicationObject.OnOpen(TimeSpan timeout)
   at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
   at System.ServiceModel.Security.CommunicationObjectSecurityTokenAuthenticator.Open(TimeSpan timeout)
   at System.ServiceModel.Security.SecurityUtils.OpenCommunicationObject(ICommunicationObject obj, TimeSpan timeout)
   at System.ServiceModel.Security.SecurityUtils.OpenTokenAuthenticatorIfRequired(SecurityTokenAuthenticator tokenAuthenticator, TimeSpan timeout)
   at System.ServiceModel.Security.SecurityProtocolFactory.Open(String propertyName, Boolean requiredForForwardDirection, SecurityTokenAuthenticator authenticator, TimeSpan timeout)
   at System.ServiceModel.Security.SymmetricSecurityProtocolFactory.OnOpen(TimeSpan timeout)
   at System.ServiceModel.Security.WrapperSecurityCommunicationObject.OnOpen(TimeSpan timeout)
   at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
   at System.ServiceModel.Security.SecurityProtocolFactory.Open(Boolean actAsInitiator, TimeSpan timeout)
   at System.ServiceModel.Security.SecurityListenerSettingsLifetimeManager.Open(TimeSpan timeout)
   at System.ServiceModel.Channels.SecurityChannelListener`1.OnOpen(TimeSpan timeout)
   at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
   at System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen(TimeSpan timeout)
   at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
   at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout)
   at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
   at System.ServiceModel.Security.SecuritySessionSecurityTokenAuthenticator.OnOpen(TimeSpan timeout)
   at System.ServiceModel.Security.WrapperSecurityCommunicationObject.OnOpen(TimeSpan timeout)
   at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
   at System.ServiceModel.Security.CommunicationObjectSecurityTokenAuthenticator.Open(TimeSpan timeout)
   at System.ServiceModel.Security.SecurityUtils.OpenCommunicationObject(ICommunicationObject obj, TimeSpan timeout)
   at System.ServiceModel.Security.SecurityUtils.OpenTokenAuthenticatorIfRequired(SecurityTokenAuthenticator tokenAuthenticator, TimeSpan timeout)
   at System.ServiceModel.Security.SecuritySessionServerSettings.OnOpen(TimeSpan timeout)
   at System.ServiceModel.Security.WrapperSecurityCommunicationObject.OnOpen(TimeSpan timeout)
   at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
   at System.ServiceModel.Security.SecuritySessionServerSettings.Open(TimeSpan timeout)
   at System.ServiceModel.Security.SecurityListenerSettingsLifetimeManager.Open(TimeSpan timeout)
   at System.ServiceModel.Channels.SecurityChannelListener`1.OnOpen(TimeSpan timeout)
   at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
   at System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen(TimeSpan timeout)
   at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
   at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout)
   at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
   at System.ServiceModel.Channels.CommunicationObject.Open()
   at Microsoft.Tools.SvcHost.ServiceHostHelper.OpenService(ServiceInfo info)
System.Net.HttpListenerException (0x80004005): Access is denied
   at System.Net.HttpListener.AddAllPrefixes()
   at System.Net.HttpListener.Start()
   at System.ServiceModel.Channels.SharedHttpTransportManager.OnOpen()

所以,我尝试按照它所说的去做:将端口更改为 8732,但仍然抛出异常,这次说Please try changing the HTTP port to 8732...HTTP could not register URL http://+:8732/EvalService/.... :P

我试过注释掉新的端点无济于事。即使是原始的也因此错误而失败。如果我把它们放回原来的端口 [不是这样] 1337 也没关系。

这是 App.config,没有什么突出的问题:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.web>
    <compilation debug="true" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="EvalServiceLibrary.EvalService">
        <clear />
        <endpoint address="ws" binding="wsHttpBinding" contract="EvalServiceLibrary.IEvalService"
          listenUriMode="Explicit">
          <identity>
            <dns value="localhost" />
            <certificateReference storeName="My" storeLocation="LocalMachine"
              x509FindType="FindBySubjectDistinguishedName" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"
          listenUriMode="Explicit">
          <identity>
              <dns value="localhost" />
              <certificateReference storeName="My" storeLocation="LocalMachine"
              x509FindType="FindBySubjectDistinguishedName" />
          </identity>
        </endpoint>
        <endpoint address="basic" binding="basicHttpBinding" contract="EvalServiceLibrary.IEvalService"
          listenUriMode="Explicit">
          <identity>
            <dns value="localhost" />
            <certificateReference storeName="My" storeLocation="LocalMachine"
              x509FindType="FindBySubjectDistinguishedName" />
          </identity>
        </endpoint>
        <endpoint address="net.tcp://localhost:8888/EvalService" binding="netTcpBinding"
          contract="EvalServiceLibrary.IEvalService" listenUriMode="Explicit">
          <identity>
            <dns value="localhost" />
            <certificateReference storeName="My" storeLocation="LocalMachine"
              x509FindType="FindBySubjectDistinguishedName" />
          </identity>
        </endpoint>
        <endpoint address="net.pipe://localhost/EvalService" binding="netNamedPipeBinding"
          bindingConfiguration="" contract="EvalServiceLibrary.IEvalService" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/EvalService" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

出了什么问题/我该如何解决?

4

6 回答 6

7

我也关注了相同的网络广播并遇到了同样的问题。我通过按照其他人的建议评论这些条目来解决证书问题。

我通过将“Design_Time_Addresses”添加回我的网址更正了“请尝试将 HTTP 端口更改为 8732 或以管理员身份运行”

不工作

<add baseAddress="http://localhost:8732/AMessageService" />

作品!

<add baseAddress="http://localhost:8732/Design_Time_Addresses/AMessageService" />
于 2012-02-27T06:05:25.693 回答
3

对于上面两个问题中的后者(HTTP could not register URL一个),一个看似可行的解决方案可以在Gabobcat 的回答(由他)的评论中发布的VS2010 培训套件中找到。

在第 4 个练习“Ex4-ServiceDiscovery”中,您将看到一个使用 WCF 服务在两个客户端之间进行通信的聊天应用程序。它使用发现功能来查找网络上的对等点。长话短说,当您最初尝试运行已完成的应用程序时(在填写练习中列出的发现事件之后)时,会发生相同的异常。与培训工具包捆绑在一起的是几个批处理脚本(AFAICT 使用 Microsoft 的“开源”许可证,因此我应该能够共享这些脚本)来设置或删除“URL ACL”。实际上,它们似乎只是一个(有用的)命令:

文件:AddURLACL.cmd

@Echo Off
@Echo Grant permissions for URL reservation
@Echo Parameter 1 "%1" == port
@Echo Parameter 2 "%2" == URL
pause
netsh http add urlacl url=http://+:%1/%2 user=%USERDOMAIN%\%USERNAME%

文件:DelURLACL.cmd

@Echo Off
@Echo Deletes permissions for URL reservation
@Echo Parameter 1 "%1" == port
@Echo Parameter 2 "%2" == URL
pause
netsh http delete urlacl url=http://+:%1/%2

显然,netsh命令是唯一重要的。示例项目让您使用以下命令(从“以管理员身份运行”命令提示符窗口)“修复”此异常:

C:\VS2010TrainingKit\Labs\WhatsNewInWCF4\Source\Setup\AddURLACL.cmd 8000

(我使用绝对路径只是为了向您展示在培训工具包中可以找到这些脚本的位置;我个人在执行此操作时位于 WhatsNewInWCF4\Source 目录中)

字符串 8000 在整个项目中仅出现 1 次:

C:\VS2010TrainingKit\Labs\WhatsNewInWCF4\Source\Ex4-ServiceDiscovery\Begin\C#>findstr /n /s "8000" *
DiscoveryChat\SimpleChat.cs:376:            this.localAddress = new Uri("http://localhost:8000/" + Guid.NewGuid().ToString());

端口 8000 似乎是聊天客户端尝试启动服务主机的端口(不要问我他们是如何共享它的……我假设ServiceHost他们使用的类足够智能,可以重用现有服务。 ..)。

我还没有对此进行彻底的试验,但它似乎已经“修复”了我的 EvalService 项目(当我将端口更改为 8000 时)。目前,大多数端点已被移除(仅basicHttpBinding剩下端点)并baseAddress设置为localhost:8000.

让我知道这是否也适合您。

于 2011-10-27T15:15:19.640 回答
1

问题出在您的配置中。您正在引用端点中的证书。要么删除它们:

<certificateReference storeName="My" storeLocation="LocalMachine" x509FindType="FindBySubjectDistinguishedName" />

或者您将一些证书放在证书存储中。我建议此时删除“身份”配置部分,直到您了解更多关于 WCF 的信息。尝试只停留在初学者的基本端点配置上,并从您了解 WCF 基础知识的角度开始工作。WCF 会根据您的特定需求为您提供大量(非常大)的配置选项。

也许这些链接可以帮助您更好地进入 WCF,而不是您一直在观看的视频:

希望这可以帮助。

于 2011-10-25T15:12:18.943 回答
1

我对同样的问题感到沮丧。最后,我通过右键单击 Visual Studio 2010 并选择“以管理员身份运行”以管理员身份运行 Visual Studio,自己找到了解决方案。

于 2012-04-08T04:59:48.380 回答
0

以为我会从我正在阅读的内容中添加一个更新的综合答案。

虽然我们都可以不遗余力地允许使用证书的权限,但大多数阅读本文的人实际上只是想尽快启动并运行 WCF 端点。

因此有几个选择。

1. sudipto-kumar-mukherjee above states "I was frustated with the same issue. 
Finally I found the solution by myself by running the Visual Studio as Administrator 
by right clicking Visual Studio 2010 and choosing 'Run As Administrator'."   
--> Yes,  this     will work.
OR
2. nasp.tech response with 
"Doesn't Work"
<add baseAddress="http://localhost:8732/AMessageService" />
"Works!"
<add baseAddress="http://localhost:8732/Design_Time_Addresses/AMessageService" />

这两种方法似乎都是解决这个问题的最快方法。

于 2013-02-25T00:57:33.033 回答
-2

要以管理员身份运行,请右键单击 Visual Studio,然后从上下文菜单中选择以管理员身份运行。要始终以管理员身份运行,请右键单击 Visual Studio,然后转到上下文菜单中的属性转到兼容性选项卡,然后选择以管理员身份运行此程序。

于 2013-12-19T03:08:07.903 回答