如何从浏览器调用WCF服务OperationContract? [英] How to call WCF service OperationContract from browser?

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

问题描述

我创建了一个 WCF 4 Web服务,并将其托管在我的 IIS 7 上,我给了以下服务网址: WCF 项目中的发布网页部分:

的http://本地主机:8084 / service1.svc



然后我绑定我发布的网站在端口: 4567 并在 IIS 中键入: http

 要检查它,我点击网站在IIS,然后点击浏览。 
它在我的浏览器中打开一个页面:



这意味着Web服务成功托管在 IIS 。现在我想调用我的实例方法并在浏览器返回输出。让我粘贴您的示例代码 Iservice.cs service.svc.cs

 #Iservice.cs:
命名空间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

 命名空间somenamespace {
内部静态类UnsafeNativeMethods
{
_dllLocation =some .dll文件;
[DllImport(_dllLocation,CallingConvention = CallingConvention.Cdecl)]
public static extern bool OnDecryption();
}

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

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



我把 some.dll 在同一个项目文件夹。我该怎么办?



编辑:



我忘了粘贴我的 web.config

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







为此我遵循这个博客:创造中的演练

解决方案

默认情况下, HTTP 被翻译成 wsHttpBinding ,这是一个 SOAP 服务,所以你不能只是从浏览器调用。帮助页面上的?wsdl 后缀也似乎指向了这个方向。



对于测试SOAP服务,您需要具有SOAP功能的工具,如Microsoft WCF测试客户端 SoapUI



尝试使用WCF测试客户端 - 可以连接在您使用该工具的帮助页面上显示的URL中提供您的服务?你看到你的服务吗?


I created a WCF 4 web services and hosted it on my IIS 7, I gave the following Service URL: at publish web section in WCF project:

http://localhost:8084/service1.svc.

Then i bind my published web site on port:4567 and Type: http in IIS.

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

It means that the web service is successfully hosted on IIS. Now i want to call my instances method and return the output at browser. Let me paste you the sample code of Iservice.cs and service.svc.cs

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

Whereas my service.svc.cs:

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

and 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();
   }}

how should i call getdata() method from browsers?

I have put the some.dll in the same project folder. Where should i do it?

Edit:

I have forgot to paste my 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" />

To do this i followed this blog : walkthrough on creating

解决方案

By default - HTTP is translated into wsHttpBinding which is a SOAP service, so you cannot just call that from a browser. The ?wsdl suffix on the help page also seems to point in that direction.

For testing SOAP services, you need a SOAP-capable tool like the Microsoft WCF Test Client or SoapUI.

Try using the WCF Test Client - can you connect to your service at the URL shown on the help page with that tool? Do you see your service?

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

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