Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在浏览器中使用WCF?_C#_.net_Wcf - Fatal编程技术网

C# 如何在浏览器中使用WCF?

C# 如何在浏览器中使用WCF?,c#,.net,wcf,C#,.net,Wcf,我想用一个GET函数构建一个简单的服务,该函数接受来自浏览器AJAX请求的JSON并返回一个文件。我可以使用的工具之一是Visual Studio 2017,WCF听起来是个不错的选择 我一直在苦苦挣扎,试图从这个框架中挤出任何功能。在线教程对我的理解来说要么太高,要么解决方案已经过时。我想要求的是一些关于WCF的基本指针,如何使用,如何不使用,与一个对网络原理知之甚少的人交谈。但这是一个相当广泛的范围 为了使这一点仅限于一个狭义的示例,这里是VisualStudio为WCF项目提供的全新源代码

我想用一个GET函数构建一个简单的服务,该函数接受来自浏览器AJAX请求的JSON并返回一个文件。我可以使用的工具之一是Visual Studio 2017,WCF听起来是个不错的选择

我一直在苦苦挣扎,试图从这个框架中挤出任何功能。在线教程对我的理解来说要么太高,要么解决方案已经过时。我想要求的是一些关于WCF的基本指针,如何使用,如何不使用,与一个对网络原理知之甚少的人交谈。但这是一个相当广泛的范围

为了使这一点仅限于一个狭义的示例,这里是VisualStudio为WCF项目提供的全新源代码模板,即开箱即用

服务1.svc:

using System;

namespace BasicWcfProject
{
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
    }
}
IService1.cs:

using System.Runtime.Serialization;
using System.ServiceModel;

namespace BasicWcfProject
{
    [ServiceContract]
    public interface IService1
    {

        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        // TODO: Add your service operations here
    }


    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
}
Web.Config:

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

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2"/>
  </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>

</configuration>

如果我在调试器中运行此程序,并将浏览器指向
http://localhost:56586/Service1.svc
,我得到基本服务响应。好的,酷。我的直觉告诉我应该访问
http://localhost:56586/Service1.svc/GetData/1
测试
GetData
方法,对吗?应该不会。我得到的回答完全是空的。我在方法中放置断点,它们不会触发。我已尝试将访问URL更改为
http://localhost:56586/Service1.svc/GetData
。我试着用一个模板来装饰这个方法。什么都不管用

很明显,我做错了什么,但我已经绝望地迷失了方向,我不知道该去哪里。我找到的任何试图告诉我该去哪里的资源要么对我来说太过技术化,要么在尝试时解决方案根本不起作用。这真的打击了我的士气


如何让web浏览器使用此WCF服务?

您已经创建了一个基于Soap的服务。URL不会更改,只会更改有效负载以获得正确的呼叫。您假设它是一个REST服务,而事实并非如此。如果你想要一个RESTful服务,我建议你看看asp.NETWebAPI,而不是WCF。尽管你可以通过WCF来实现REST,如果你愿意的话,这给了我比谷歌数小时搜索更多的信息。谢谢你。编辑:过早发送。我已经尝试了一个ASP.NET服务,看,我不能用这个实现自定义JSON反序列化程序,或者至少,没有人告诉我如何实现。WCF似乎是唯一的选择。好吧,我没有时间回答问题,但我已经非常轻松地使用ASP.net核心创建了一个Rest API,它使用的是yeoman(aspnet)模板,而不是依赖Visual Studio。我还使用
dotnet运行