如何从浏览器调用WCF服务OperationContract?

时间:2013-01-04 15:13:44

标签: c# visual-studio-2010 wcf dll iis-7

我创建了WCF 4个网络服务并将其托管在我的IIS 7上,我在Service URL:项目的发布网站部分提供了以下WCF

http://localhost:8084/service1.svc

然后我将已发布的web site绑定到端口4567,并在http中输入IIS

To checked it i clicked on `web site` at `IIS` and click on the browse. 
It open a following page at my browser:

enter image description here

这意味着Web服务已成功托管在IIS上。现在我想调用我的实例方法并在浏览器中返回输出。让我粘贴一下Iservice.csservice.svc.cs

的示例代码
#Iservice.cs:
namespace some.decryption
{
[ServiceContract]
public interface Iservice
{
  [OperationContract, WebInvoke(Method = "GET", UriTemplate = "/getdata", ResponseFormat = WebMessageFormat.Json)]
    string getdata();
}}

而我的service.svc.cs

public bool getdata()
    {
        return somenamespace.getfetcheddll();
    }

serviceDll.cs

  namespace somenamespace{
  internal static class UnsafeNativeMethods
   {
   _dllLocation = "some.dll";
   [DllImport(_dllLocation, CallingConvention = CallingConvention.Cdecl)]
   public static extern bool OnDecryption();
   }

  public static string getfetcheddll()
   {
       return UnsafeNativeMethods.OnDecryption();
   }}

我应该如何从浏览器调用getdata()方法?

我已将some.dll放在同一项目文件夹中。我应该在哪里做?

编辑:

我忘了粘贴web.config

<?xml version="1.0"?>
<configuration>
<system.web>
 <compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
  <behaviors>
  <serviceBehaviors>
    <behavior>
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

          

要做到这一点,我按照这个博客:walkthrough on creating

1 个答案:

答案 0 :(得分:4)

默认情况下,HTTP已转换为wsHttpBinding这是 SOAP 服务,因此您无法只能从浏览器中调用该服务。帮助页面上的?wsdl后缀似乎也指向了这个方向。

要测试SOAP服务,您需要一个支持SOAP的工具,如Microsoft WCF Test ClientSoapUI

尝试使用WCF测试客户端 - 您是否可以使用该工具通过帮助页面上显示的URL连接到您的服务?你看到了你的服务吗?