VB.NET解决方案 - “WCF服务默认项目设置示例不起作用”

时间:2013-12-13 06:04:10

标签: vb.net wcf web-services visual-studio-2012

我知道之前已经问过这个问题,我已经通过精细的牙齿梳理了这里唯一回答的问题中的2个,并且在几个小时之后尝试了所有可能的解决方案,我仍然无法获得默认的WCF项目在VS2012上工作(参见标题中的错误)。

简而言之: 我想要做的就是让默认的WCF应用程序项目在VisualStudio 2012 Pro中运行。

我尝试过的: 更改WEB.CONFIG结束以添加以下内容(这显然适用于大多数人,但不适用于我)

<services>
<service behaviorConfiguration="metadataBehavior" name="Service1">
  <endpoint
      address=""
      binding="customBinding" bindingConfiguration="jsonpBinding"
      contract="Service1.IService1"/>
  <endpoint 
    address="mex"
    binding="mexHttpBinding" 
    contract="IMetadataExchange" />
</service>

以下内容:

  <behaviors>
<serviceBehaviors>
  <behavior name="metadataBehavior">
    <serviceMetadata httpGetEnabled="true" />
  </behavior>
</serviceBehaviors>

iService1.vb代码(未更改默认值):

<ServiceContract()>
Public Interface IService1

    <OperationContract()>
    Function GetData(ByVal value As Integer) As String

    <OperationContract()>
    Function GetDataUsingDataContract(ByVal composite As CompositeType) As CompositeType

    ' TODO: Add your service operations here

End Interface


<DataContract()>
Public Class CompositeType

<DataMember()>
Public Property BoolValue() As Boolean

<DataMember()>
Public Property StringValue() As String

End Class

Service1.svc的标记:

    <%@ ServiceHost Language="VB" Debug="true" Service="WcfService1.Service1" CodeBehind="Service1.svc.vb" %>

顺便提一下,有一条长蓝色波浪线表示“无法识别的配置部分服务。(E:\ projects \ TestJquery \ WcfService1 \ web.config第35行)”所以我在这里需要其他东西 - 另一个项目参考?

这是完整的web.config文件(唯一改变的是建议添加“服务”部分)

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

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </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>
  <services>
    <service behaviorConfiguration="metadataBehavior" name="Service1">
      <endpoint
          address=""
          binding="customBinding" bindingConfiguration="jsonpBinding"
          contract="Service1.IService1"/>
      <endpoint 
        address="mex"
        binding="mexHttpBinding" 
        contract="IMetadataExchange" />
    </service>
  </services>
  <behaviors>
    <serviceBehaviors>
      <behavior name="metadataBehavior">
        <serviceMetadata httpGetEnabled="true" />
      </behavior>
    </serviceBehaviors>
  </behaviors>
</configuration>

我想,如果我能让默认的WCF webservice项目工作,我可以更好地剖析并理解发生了什么。如果有人这样做,请发送您正在运行的,有效的默认VS2012解决方案(或链接)的zip文件。我真的想摆脱ASMX网络服务,WCF的词典似乎没有任何意义。

web.config更改 - 仍然没有工作,但没有蓝色波浪形:)

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

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
        <behavior name="metadataBehavior">
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="metadataBehavior"  name="WcfService1.Service1">
        <endpoint
            address=""
            binding= "basicHttpBinding"
           />
        <endpoint
          address="mex"
          binding="mexHttpBinding"
          contract="IMetadataExchange" />
      </service>
    </services>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </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>

FINAL WEB.CONFIG默认设置,开箱即用,VS2012 WCF Web Service应用程序工作。我希望这能帮助像我这样的所有新手在学习WCF时至少有一些调整内容:

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

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
        <behavior name="metadataBehavior">
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="metadataBehavior" name="WcfService1.Service1">
        <endpoint
            address=""
            binding= "basicHttpBinding"
            contract="WcfService1.IService1"
           />
        <endpoint
          address="mex"
          binding="mexHttpBinding"
          contract="IMetadataExchange" />
      </service>
    </services>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
      <add binding="basicHttpBinding" scheme="http" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </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>

1 个答案:

答案 0 :(得分:1)

好的,所以有很多事情。正如我在评论中所说的那样,项目应该可以正常运行而不需要更改。

您发布的代码中的主要问题是您的web.config不正确 - 您在<services>部分之外有<behaviors><system.serviceModel>部分,这就是您的原因重新获得你所看到的错误。此外,您的行为的结束标记顺序错误。

您的配置应该的样子是这样的:

<system.serviceModel>
  <bindings>
    <wsHttpBinding>
      <binding name="MyServiceBinding" />
    </wsHttpBinding>
  </bindings>
  <services>
    <service behaviorConfiguration="metadataBehavior" name="WcfService1.Service1">
      <endpoint address=""
                binding="wsHttpBinding" 
                bindingConfiguration="MyServiceBinding"
                contract="WcfService1.IService1"/>
      <endpoint address="mex"
                binding="mexHttpBinding" 
                contract="IMetadataExchange" />
    </service>
  </services>
  <behaviors>
    <serviceBehaviors>
      <behavior name="metadataBehavior">
        <serviceMetadata httpGetEnabled="true" />
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <behaviors>
    <serviceBehaviors>
      <behavior>
        <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
        <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
        <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
        <serviceDebug includeExceptionDetailInFaults="false"/>
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <protocolMapping>
    <add binding="basicHttpsBinding" scheme="https" />
  </protocolMapping>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>

请注意,我删除了您的自定义绑定,并且为了说明的目的,使用了wsHttpBinding并命名了配置MyServiceBinding。这将导致服务使用该配置部分来绑定信息(因为我没有设置任何值,它将是该绑定的默认值。)

您可以删除配置的整个服务模型部分并点击F5,您将获得通过默认basicHttpBinding端点公开的服务。

<强>加

WCF很复杂,学习曲线陡峭。仅绑定配置是一个很大的主题,可能是WCF中开发人员遇到问题的最大原因之一。记住WCF的ABC - 地址,绑定和合同。 必须让所有三个人都能成功运行服务。

使用WCF 4.0 Microsoft实现了默认端点,绑定等概念,使配置更容易,并且他们在4.5中添加了其他内容。看一下A Developer's Introduction to Windows Communication Foundation 4作为首发。