如何从浏览器访问WCF服务的方法呢? [英] How to access WCF service methods from browser?

查看:1235
本文介绍了如何从浏览器访问WCF服务的方法呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是这是在Visual Studio 2013在默认情况下向导创建一个code。
在项目属性我设置为使用本地IIS。
WCF测试客户端成功地对其进行测试。
但是,如果我访问页

  HTTP://localhost/WcfService1/Service1.svc/GetTime

在浏览器的话,我看空浏览器屏幕和小提琴手显示HTTP / 1.1 400错误的请求。
我知道我需要修改web.config文件,并在接口中的方法属性,但是不知道怎么办。你可以帮帮我吗?谢谢你。

IService1.cs

 使用System.Runtime.Serialization;
使用System.ServiceModel;
命名空间WcfService1
{
    [服务合同]
    公共接口IService1
    {
        [OperationContract的]
        串GETTIME();
    }
}

Service1.svc.cs

 使用系统;
命名空间WcfService1
{
    公共类服务1:IService1
    {
        公共字符串GETTIME()
        {
            返回DateTime.Now.ToShortTimeString();
        }
    }
}

的web.config

 <?XML版本=1.0&GT?;
<结构>  <&的appSettings GT;
    <添加键=ASPNET:UseTaskFriendlySynchronizationContextVALUE =真/>
  < /的appSettings>
  <&的System.Web GT;
    <编译调试=真targetFramework =4.5/>
    <的httpRuntime targetFramework =4.5/>
  < /system.web>
  < system.serviceModel>
    <&行为GT;
      < serviceBehaviors>
        <&行为GT;
          < serviceMetadata httpGetEnabled =真httpsGetEnabled =真/>
          < serviceDebug includeExceptionDetailInFaults =FALSE/>
        < /行为>
      < / serviceBehaviors>
    < /行为>
    < protocolMapping>
        <添加绑定=basicHttpsBinding计划=HTTPS/>
    < / protocolMapping>
    < serviceHostingEnvironment aspNetCompatibilityEnabled =真multipleSiteBindingsEnabled =真/>
  < /system.serviceModel>
  < system.webServer>
    <模块runAllManagedModulesForAllRequests =真/>
    < directoryBrowse启用=真/>
  < /system.webServer>< /结构>


解决方案

感谢您sakir和蒂姆,你的评论让我寻找在适当的地方的答案。是的,这是SOAP服务,因此我需要通过网络的HTTP,以重新配置它来访问。


  1. 我说的这种行为,让HttpBinding在web.config中节的行为,并配置

  2. 我添加的属性[WebGet]来,我想在浏览器中,使可见的方法。

请注意:类似的结果,我们可以在Visual Studio中得到的,如果我们将通过空项目将创建WCF Web服务WCF服务(阿贾克斯启用)模板(我已经很难找到在VS2013,但它是在顶部VS2012作为一个单独的项目)。这将修改web.config中为我们,并给予工作code例子(但不是基于接口)。

修改的文件,并重新配置如下web.config中:

IService1.cs

 使用System.Runtime.Serialization;
使用System.ServiceModel;
使用System.ServiceModel.Web;
命名空间WcfService1
{
    [服务合同]
    公共接口IService1
    {
        [OperationContract的]
        [WebGet]
        串GETTIME();
    }
}

的web.config

 <?XML版本=1.0编码=UTF-8&GT?;
<结构>  <&的appSettings GT;
    <添加键=ASPNET:UseTaskFriendlySynchronizationContextVALUE =真/>
  < /的appSettings>
  <&的System.Web GT;
    <编译调试=真targetFramework =4.5/>
    <的httpRuntime targetFramework =4.5/>
  < /system.web>
  < system.serviceModel>
    <&行为GT;
      < serviceBehaviors>
        <&行为GT;
          < serviceMetadata httpGetEnabled =真httpsGetEnabled =真/>
          < serviceDebug includeExceptionDetailInFaults =真/>
        < /行为>
      < / serviceBehaviors>      **<! - 添加行为 - >
      < endpointBehaviors>
        <行为NAME =WcfService1.Service1AspNetAjaxBehavior>
          < enableWebScript />
        < /行为>
      < / endpointBehaviors> **    < /行为>
    < protocolMapping>
        <添加绑定=basicHttpsBinding计划=HTTPS/>
    < / protocolMapping>    < serviceHostingEnvironment aspNetCompatibilityEnabled =真multipleSiteBindingsEnabled =真/>    **<! - 增值服务行为的配置 - >
    <服务和GT;
      <服务名称=WcfService1.Service1>
        <端点地址=behaviorConfiguration =WcfService1.Service1AspNetAjaxBehavior
          绑定=的WebHttpBinding合同=WcfService1.IService1/>
      < /服务>
    < /服务> **  < /system.serviceModel>
  < system.webServer>
    <模块runAllManagedModulesForAllRequests =真/>
    < directoryBrowse启用=真/>
  < /system.webServer>< /结构>

It's a code that was created in Visual Studio 2013 by default wizard. In project properties I set to use Local IIS. WCF Test Client test it successfully. But if I access page

http://localhost/WcfService1/Service1.svc/GetTime

in browser then I see empty browser screen and Fiddler show "HTTP/1.1 400 Bad Request". I understand that I need to modify web.config, and attributes for the method in interface but don't know how. Could you help me? Thank you.

IService1.cs

using System.Runtime.Serialization;
using System.ServiceModel;
namespace WcfService1
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetTime();
    }
}

Service1.svc.cs

using System;
namespace WcfService1
{
    public class Service1 : IService1
    {
        public string GetTime()
        {
            return DateTime.Now.ToShortTimeString();
        }
    }
}

web.config

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

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <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"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

解决方案

Thank you to sakir and Tim, you comments allow me to look for answer in proper places. Yes, this is SOAP service, so I need to reconfigure it in order to access by web http.

  1. I added section "behavior" and configuration of this behavior that allow HttpBinding in web.config
  2. I added attribute [WebGet] to the method that I want to make visible in the browser.

Note: similar result we can get in Visual Studio, if we will create WCF web service by adding in empty project "WCF Service (Ajax-enabled)" template (which I had hard time finding in VS2013, but it's on the top in VS2012 as a separate project). This will modify web.config for us, and give working code example (however not based on interface).

Modified files and reconfigured web.config below:

IService1.cs

using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
namespace WcfService1
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebGet]
        string GetTime();
    }
}

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" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>

      **<!--added behavior-->
      <endpointBehaviors>
        <behavior name="WcfService1.Service1AspNetAjaxBehavior">
          <enableWebScript />
        </behavior>
      </endpointBehaviors>**

    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

    **<!--added service behaviour configuration-->
    <services>
      <service name="WcfService1.Service1">
        <endpoint address="" behaviorConfiguration="WcfService1.Service1AspNetAjaxBehavior"
          binding="webHttpBinding" contract="WcfService1.IService1" />
      </service>
    </services>**

  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

这篇关于如何从浏览器访问WCF服务的方法呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆