【问题标题】:WCF WPF Web Service Client throwing 404WCF WPF Web服务客户端抛出404
【发布时间】:2025-12-17 16:30:01
【问题描述】:

我知道有很多关于该错误问题的问题,但我无法摆脱它。

当我在导航器中编写地址时,它可以工作,但是当我在 WPF 应用程序中调用相同的方法时,出现 404 错误。

这是我的 WPF 应用程序的服务模式:

<system.serviceModel>
 <behaviors>
  <endpointBehaviors>
    <behavior name="webEndpoint">
      <webHttp defaultBodyStyle="Wrapped"
               defaultOutgoingResponseFormat="Xml"
               helpEnabled="true"/>
    </behavior>
  </endpointBehaviors>
 </behaviors>
 <bindings>
  <webHttpBinding>
    <binding name="webHttp">
      <security mode="None">
        <transport clientCredentialType="None" />
      </security>
    </binding>
  </webHttpBinding>
 </bindings>
 <client>
  <endpoint binding="webHttpBinding"
            bindingConfiguration="webHttp"
            behaviorConfiguration="webEndpoint"  
            contract="ServiceReferenceBX.IServiceBX"
            address="http://localhost:9804/ServiceBX.svc">
  </endpoint>
 </client>
</system.serviceModel>`   

这是我的 WCF 的 web.config

`<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5.2"/>
<httpRuntime targetFramework="4.5.2"/>
<httpModules>
  <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"/>
</httpModules>
</system.web>
<system.serviceModel>
<services>
  <service name="BXSportWCFLib.ServiceBX" behaviorConfiguration="ServiceBehaviour">
    <!-- Service Endpoints -->
    <!-- Unless fully qualified, address is relative to base address supplied above -->
    <endpoint address ="" binding="webHttpBinding" contract="BXSportWCFLib.IServiceBX" behaviorConfiguration="web">
    </endpoint>
  </service>
</services>

<behaviors>
  <serviceBehaviors>
    <behavior name="ServiceBehaviour">
      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="web">
      <webHttp/>
    </behavior>
  </endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
  <remove name="ApplicationInsightsWebTracking"/>
  <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"
    preCondition="managedHandler"/>
</modules>
<directoryBrowse enabled="true"/>
<validation validateIntegratedModeConfiguration="false"/>
</system.webServer>
</configuration>`

这是我的合同

[ServiceContract]
public interface IServiceBX
{
    [OperationContract]
    [WebInvoke(Method = "GET",
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.WrappedRequest,
        UriTemplate = "ListExercices/{guidUser}")]
    List<Exercice> getListExercices(string guidUser);

    [OperationContract]
    [WebInvoke(Method = "POST",
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    Exercice AddNewExercice(string guidUser,string nom,bool nbRepetition, bool Poids, bool Distance, bool Duree);

    [OperationContract]
    [WebInvoke(Method = "GET",
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.WrappedRequest,
        UriTemplate = "ListeFen/{guidUser}")]
    List<Fenetre> getFenetres(string guidUser);

    [OperationContract]
    [WebInvoke(Method = "GET",
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.WrappedRequest,
        UriTemplate = "Connect/{login}/{mdpCrypt}")]
    string UserConnect(string login, string mdpCrypt);

}

我使用 UserConnect 方法进行了测试。

非常感谢。

【问题讨论】:

    标签: wpf wcf web-config client endpoint


    【解决方案1】:

    我的 app.config

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
    <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="BXSportCL.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
    </configSections>
    <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
    <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="soap" 
                 maxReceivedMessageSize="1116553600"
                 maxBufferPoolSize="1116553600"
                 maxBufferSize="1116553600"/>
      </basicHttpBinding>
      <wsHttpBinding>
        <binding name="mex" maxReceivedMessageSize="1116553600"
                 maxBufferPoolSize="1116553600">
          <security mode="None" />
        </binding>
      </wsHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:9804/ServiceBX.svc/soap"
        binding="basicHttpBinding" bindingConfiguration="soap" contract="ServiceReferenceBX.ServiceBX"
        name="soap" />
    </client>
    </system.serviceModel>
    <applicationSettings>
    <BXSportCL.Properties.Settings>
      <setting name="DebugMode" serializeAs="String">
        <value>True</value>
      </setting>
    </BXSportCL.Properties.Settings>
    </applicationSettings>
    </configuration>
    

    这里是我的 web.config:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
    <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
    <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
    </appSettings>
    
    <connectionStrings>
    <add name="ModelBXContainer" connectionString="metadata=res://*/ModelBX.csdl|res://*/ModelBX.ssdl|res://*/ModelBX.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=GOBELET-PC\SQLEXPRESS;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    </connectionStrings>
    <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
    </entityFramework>
    <system.web>
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2" />
    <httpModules>
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
    </httpModules>
    </system.web>
    <system.serviceModel>
    <services>
      <service name="BXSportWCFLib.ServiceBX" behaviorConfiguration="MyServiceBehavior">
        <endpoint name="rest" address="" binding="webHttpBinding" contract="BXSportWCFLib.ServiceBX" behaviorConfiguration="restBehavior" />
        <endpoint name="mex" address="mex" binding="mexHttpBinding" contract="BXSportWCFLib.ServiceBX" />
        <endpoint name="soap" address="soap" binding="basicHttpBinding" contract="BXSportWCFLib.ServiceBX" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MyServiceBehavior" >
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="restBehavior">
          <webHttp  />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    </system.serviceModel>
    <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <remove name="ApplicationInsightsWebTracking" />
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
    </modules>
    <directoryBrowse enabled="true" />
    <validation validateIntegratedModeConfiguration="false" />
    </system.webServer>
    </configuration>
    

    这是我的合同:

    [ServiceContract(Name = "ServiceBX", Namespace = "http://ServiceBX/", SessionMode = SessionMode.NotAllowed)]
    //[ServiceKnownType(typeof (IList<MyDataContractTypes>))]
    [ServiceBehavior(Name = "ServiceBX", Namespace = "http://ServiceBX/")]
    public class ServiceBX
    {
        [OperationContract(Name = "Fenetres")]
        [WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "Fenetres/{guidUser}")]
        public string getFenetres(string guidUser)
        {
            JsonSerializerSettings settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All };
            return JsonConvert.SerializeObject(DAL.User.getFenetres(guidUser), settings);
            //return SerialJSON.WriteFromObject(DAL.User.getFenetres(guidUser),typeof(List<Fenetre>));
        }
    }
    

    【讨论】:

    • edit 提供更多信息。纯代码和“试试这个”的答案是discouraged,因为它们不包含可搜索的内容,也没有解释为什么有人应该“试试这个”。我们在这里努力成为知识的资源。
    • 不知道多写什么,我把代码都改了。
    • 您的工作正常了,这很棒。但这是您帮助未来用户遇到类似问题的机会。所以你能解释一下:你改变了什么?旧代码有什么问题?这些变化如何解决这些具体问题?这样,其他人也许可以从中学习以解决他们的问题。