Web(ASP.Net)Wcf:未找到从桌面客户端连接时出错

问题描述 投票:0回答:1

我正在建立一个ASP.Net WebForms子域,其中WCF托管在共享主机帐户上。 WCF是该网站目前的主要目的。我无法在桌面上建立链接,从而产生一个永久性的链接

无法连接到远程服务器

我正在尝试从Add Service Reference和C#。我想在客户端的C#代码。

我一直在玩端口号和不同类型的绑定,但没有任何作用。

Web.config文件:

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.6.2"/>
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701"/>
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
    </compilers>
  </system.codedom>
    <system.serviceModel>
        <services>
            <service name="MyCloud.Communication.CloudService" behaviorConfiguration="BehaviorMyCloudService">
                <endpoint address=""
                                    binding="wsHttpBinding"
                                    bindingConfiguration="EndpointBindingMyCloudService"
                                    contract="MyCloud.Communication.ICloudService"
                                    behaviorConfiguration="web"
                                    >
                </endpoint>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost/MyCloudService/"/>
                    </baseAddresses>
                </host>
            </service>
        </services>
        <behaviors>
            <serviceBehaviors>
                <behavior name="BehaviorMyCloudService">
                    <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="true" />
                    <dataContractSerializer maxItemsInObjectGraph="2147483647" />
                    <useRequestHeadersForMetadataAddress>
                        <defaultPorts>
                            <add scheme="http" port="8080" />
                        </defaultPorts>
                    </useRequestHeadersForMetadataAddress>
                </behavior>
                <behavior name="">
                    <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="false" />
                </behavior>
            </serviceBehaviors>
            <endpointBehaviors>
                <behavior name="web">
                    <webHttp />
                </behavior>
            </endpointBehaviors>
        </behaviors>
        <bindings>
            <wsHttpBinding>
                <binding
                    name="EndpointBindingMyCloudService"
                    allowCookies="false"
                    closeTimeout="00:01:00"
                    openTimeout="00:01:00"
                    receiveTimeout="00:10:00"
                    sendTimeout="00:01:00"
                    maxBufferPoolSize="524288"
                    maxReceivedMessageSize="2147483647"
                    messageEncoding="Text"
                    textEncoding="utf-8"
                    transactionFlow="false"
                    useDefaultWebProxy="true"
                    >
                    <security mode="None">
                    </security>
                </binding>
            </wsHttpBinding>
        </bindings>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true">
            <baseAddressPrefixFilters>
                <add prefix="http://localhost:8080"/>
            </baseAddressPrefixFilters>
        </serviceHostingEnvironment>
    </system.serviceModel>
</configuration>

CloudService.svc(C#代码):

using System;
using System.IO;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using System.Web;

namespace MyCloud.Communication
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class CloudService : ICloudService, IErrorHandler
    {
        public UInt16 GetServiceId()
        {
            return MyCloudLibrary.Constants.My_Id_CloudService;
        }
    }
}

ICloudService.cs:

using System;
using System.IO;
using System.ServiceModel;
using System.ServiceModel.Web;

namespace MyCloud.Communication
{
    [ServiceContract(SessionMode = SessionMode.Allowed/*, CallbackContract = typeof(ICallbackCloudService)*/)]
    public interface ICloudService
    {
        [OperationContract(IsOneWay = false)]
        [FaultContractAttribute(typeof(CloudServiceFault))]
        UInt16 GetServiceId();
    }
}

客户端C#代码:

WSHttpBinding binding = new WSHttpBinding();
binding.Security.Mode = SecurityMode.None;
EndpointAddress endpointAddress = new EndpointAddress("http://Mycloud.asdfmydomain.com/MyCloudService");
var pipeFactory = new ChannelFactory<ICloudService>(binding, endpointAddress);
Program.HostIpcCloudService = pipeFactory.CreateChannel();
Program.HostIpcCloudService.Connect();
CommCloudService.IsPipeCloudService = true;
UInt16 serviceId = Program.HostIpcCloudService.GetServiceId();

客户端C#错误:

OuterException:

“在http://mycloud.asdfmydomain.com/MyCloudService上没有可以接受该消息的端点。这通常是由不正确的地址或SOAP操作引起的。有关详细信息,请参阅InnerException(如果存在)。”

的InnerException:

“远程服务器返回错误:(404)Not Found。”

来自Add Service ReferenceVisual Studio 2017也说了同样的话。

我从我的Windows服务中获得了类似的代码,所以可能问题是我要跨机器,可能是端口,可能是托管帐户上的防火墙等。

一些(但不是全部)参考文献:

思考?

基于答案1的更新:

(因此,我不得不忍受痛苦和痛苦的费用,因为我不得不忍受这个令人讨厌的庆祝愚人节的主题。我正在努力专注于编程并保持专注。)

我正在取得进步,没有骰子。

我的没有服务发现错误消息和重复的MyCloud.Communication.MyCloud而不是MyCLoud.Communication.CloudService错误消息是Visual Studio的不完整重构的结果。 Visual Studio没有重构.SVC标记文件。

<%@ ServiceHost Language="C#" Debug="true" Service="MyCloud.Communication.CloudService" CodeBehind="CloudService.svc.cs" %>

在浏览器中指定以下URL产生:

http://mycloud.asdfmydomain.com/Communication/CloudService.svc/Address1

以下是Microsoft link的URL的关键部分:

或者,您可以为服务的ServiceHost提供基址,然后为与此服务关联的每个端点指定相对于此基址定义的地址。路径:/mathservice.svc/secureEndpoint

我不得不删除注释掉的<serviceHostingEnvironment,因为浏览器需要multipleSiteBindingsEnabled="true"

文本说URI的第一部分是path to the SVC file,后面是endpoint address name标签中指定的EndPointAddres

错误通过Chrome:

[ServiceActivationException: The service '/Communication/CloudService.svc' cannot be activated due to an exception during compilation.  The exception message is: The endpoint at 'http://mycloud.asdfmydomain.com/Communication/CloudService.svc/Address1' does not have a Binding with the None MessageVersion.  'System.ServiceModel.Description.WebHttpBehavior' is only intended for use with WebHttpBinding or similar bindings..]

通过C#出错:

{"The requested service, 'http://mycloud.asdfmydomain.com/Communication/CloudService.svc/Address1' could not be activated. See the server's diagnostic trace logs for more information."}

我正在取得进展。

c# .net web-services wcf web-config
1个回答
0
投票

在你写的web.config中

 <baseAddresses>
                    <add baseAddress="http://localhost/MyCloudService/"/>
                </baseAddresses>

当您调用该服务时,地址为http://Mycloud.asdfmydomain.com/MyCloudService,它与您的web.config中的地址配置不匹配。

如果您不确定服务的地址是什么,可以通过wsdl查看。

例如,我的web.config是

<service name="Service.CalculatorService" >
    <endpoint address="Calculator.svc/abc/" binding="basicHttpBinding"  contract="ServiceInterface.ICalculatorService" ></endpoint>
   <host>
      <baseAddresses>
        <add baseAddress="http://localhost:62193/"/>
      </baseAddresses>
    </host>
  </service>

然后,当我输入我的服务的wsdl地址http://localhost:62193/Calculator.svc?singleWsdl的地址。

我可以通过底部的wsdl找到我的服务地址。 enter image description here

你甚至可以看到我只写了一个Calculator.svc,还有两个Calculator.svc。

因为baseaddress将首先与你的svc结合,然后与相对地址结合使用。所以最终地址将是baseaddree + svc + relativeaddress。

请参阅下面的链接WCF: relativeAddress,baseAddress and binding

© www.soinside.com 2019 - 2024. All rights reserved.