微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ServiceHost仅支持类服务类型 wcf

如何解决ServiceHost仅支持类服务类型 wcf

我刚刚创建了一个Web应用程序。和我使用WCF服务文件。我怎么无法从此本地Web服务获取服务引用。

这是我的屏幕截图:

enter image description here

这是我的本地wcf服务文件保持不变:

<%@ ServiceHost Language="C#" Debug="true" Service="Admin_Staff_Care.SC_WCF.IRestServiceImpl" CodeBehind="~/App_Code/RestServiceImplClient.cs" %>

但是我不愿意得到这些方法。使用“项目”>“添加Web参考”>,然后按照步骤操作:

enter image description here

之后,我扩展服务,然后显示此消息,如:

There was an error downloading 'http://localhost:4870/RestServiceImplClient.svc/$Metadata'.
The request Failed with the error message:
--
<!DOCTYPE html>
<html>
    <head>
        <title>ServiceHost only supports class service types.</title>
        <Meta name="viewport" content="width=device-width" />
        <style>
         body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;} 
         p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}
         b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}
         H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }
         H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }
         pre {font-family:"Consolas","Lucida Console",Monospace;font-size:11pt;margin:0;padding:0.5em;line-height:14pt}
         .marker {font-weight: bold; color: black;text-decoration: none;}
         .version {color: gray;}
         .error {margin-bottom: 10px;}
         .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:pointer; }
         @media screen and (max-width: 639px) {
          pre { width: 440px; overflow: auto; white-space: pre-wrap; word-wrap: break-word; }
         }
         @media screen and (max-width: 479px) {
          pre { width: 280px; }
         }
        </style>
    </head>

    <body bgcolor="white">

            <span><H1>Server Error in '/' Application.<hr width=100% size=1 color=silver></H1>

            <h2> <i>ServiceHost only supports class service types.</i> </h2></span>

            <font face="Arial,Helvetica,Geneva,SunSans-Regular,sans-serif ">

            <b> Description: </b>An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

            <br><br>

            <b> Exception Details: </b>System.ArgumentException: ServiceHost only supports class service types.<br><br>

            <b>Source Error:</b> <br><br>

            <table width=100% bgcolor="#ffffcc">
               <tr>
                  <td>
                      <code>

An unhandled exception was generated during the execution of the current web request. information regarding the origin and location of the exception can be identified using the exception stack trace below.                      </code>

                  </td>
               </tr>
            </table>

            <br>

            <b>Stack Trace:</b> <br><br>

            <table width=100% bgcolor="#ffffcc">
               <tr>
                  <td>
                      <code><pre>

[ArgumentException: ServiceHost only supports class service types.]
   System.ServiceModel.Description.ServiceDescription.GetService(Type serviceType) +13498366
   System.ServiceModel.ServiceHost.CreateDescription(IDictionary`2& implementedContracts) +60
   System.ServiceModel.ServiceHostBase.InitializeDescription(UriSchemeKeyedCollection baseAddresses) +148
   System.ServiceModel.ServiceHost.InitializeDescription(Type serviceType,UriSchemeKeyedCollection baseAddresses) +49
   System.ServiceModel.ServiceHost..ctor(Type serviceType,Uri[] baseAddresses) +153
   System.ServiceModel.Activation.ServiceHostFactory.CreateServiceHost(Type serviceType,Uri[] baseAddresses) +34
   System.ServiceModel.Activation.ServiceHostFactory.CreateServiceHost(String constructorString,Uri[] baseAddresses) +538
   System.ServiceModel.HostingManager.CreateService(String normalizedVirtualPath,EventTraceActivity eventTraceActivity) +1489
   System.ServiceModel.HostingManager.ActivateService(ServiceActivationInfo serviceActivationInfo,EventTraceActivity eventTraceActivity) +53
   System.ServiceModel.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath,EventTraceActivity eventTraceActivity) +666

[ServiceActivationException: The service '/RestServiceImplClient.svc' cannot be activated due to an exception during compilation.  The exception message is: ServiceHost only supports class service types..]
   System.Runtime.AsyncResult.End(IAsyncResult result) +513025
   System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult result) +182
   System.ServiceModel.Activation.ServiceHttpModule.EndProcessRequest(IAsyncResult ar) +365683
   System.Web.AsyncEventExecutionStep.InvokeEndHandler(IAsyncResult ar) +165
   System.Web.AsyncEventExecutionStep.OnAsyncEventCompletion(IAsyncResult ar) +9930149
</pre>                      </code>

                  </td>
               </tr>
            </table>

            <br>

            <hr width=100% size=1 color=silver>

            <b>Version information:</b> Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.8.4250.0

            </font>

    </body>
</html>
<!-- 
[ArgumentException]: ServiceHost only supports class service types.
   at System.ServiceModel.Description.ServiceDescription.GetService(Type serviceType)
   at System.ServiceModel.ServiceHost.CreateDescription(IDictionary`2& implementedContracts)
   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 System.ServiceModel.Activation.ServiceHostFactory.CreateServiceHost(Type serviceType,Uri[] baseAddresses)
   at System.ServiceModel.Activation.ServiceHostFactory.CreateServiceHost(String constructorString,Uri[] baseAddresses)
   at System.ServiceModel.ServiceHostingEnvironment.HostingManager.CreateService(String normalizedVirtualPath,EventTraceActivity eventTraceActivity)
   at System.ServiceModel.ServiceHostingEnvironment.HostingManager.ActivateService(ServiceActivationInfo serviceActivationInfo,EventTraceActivity eventTraceActivity)
   at System.ServiceModel.ServiceHostingEnvironment.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath,EventTraceActivity eventTraceActivity)
[ServiceActivationException]: The service '/RestServiceImplClient.svc' cannot be activated due to an exception during compilation.  The exception message is: ServiceHost only supports class service types..
   at System.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult result)
   at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult result)
   at System.ServiceModel.Activation.ServiceHttpModule.EndProcessRequest(IAsyncResult ar)
   at System.Web.HttpApplication.AsyncEventExecutionStep.InvokeEndHandler(IAsyncResult ar)
   at System.Web.HttpApplication.AsyncEventExecutionStep.OnAsyncEventCompletion(IAsyncResult ar)
-->
--.
Metadata contains a reference that cannot be resolved: 'http://localhost:4870/RestServiceImplClient.svc'.
The requested service,'http://localhost:4870/RestServiceImplClient.svc' Could not be activated. See the server's diagnostic trace logs for more information.
If the service is defined in the current solution,try building the solution and adding the service reference again.

我如何解决此问题,请帮助我。...

WCF项目app.config的第二次更新

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.7.2" />
    <httpRuntime targetFramework="4.7.2"/>
  </system.web>

  <system.serviceModel>
    <services>
      <service name="WcfService5.Service1"
               behaviorConfiguration="CalculatorServiceBehavior">
        <endpoint address=""
                  binding="basicHttpBinding"
                  contract="WcfService5.IService1"
                  bindingConfiguration="LargeWeb"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="CalculatorServiceBehavior">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>


    <bindings>
      <basicHttpBinding>
        <binding name="LargeWeb" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
          <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNaMetableCharCount="2147483647" />
        </binding>
      </basicHttpBinding>
    </bindings>


  </system.serviceModel>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging,set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directorybrowse enabled="true"/>
  </system.webServer>

</configuration>

Web应用程序Web.config的第二次更新

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IService1"/>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://desktop-684ueha:8000/Service1.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService1" contract="IService1" name="BasicHttpBinding_IService1"/>
    </client>
  </system.serviceModel>

解决方法

此错误是因为您在本地wcf服务文件中输入了错误的调试属性值,此处debug的属性值应该是已实现的服务类类型,而不是服务接口。

enter image description here

将图中的IRestServiceImpl更改为实现此接口的服务类。

您可以先将wcf服务发布到iis,以测试该服务是否可以正常调用。然后将服务引用添加到客户端。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。