【问题标题】:'Endpoint not found.' while consuming rest service of WCF through browser“未找到端点。”同时通过浏览器消费WCF的休息服务
【发布时间】:2016-11-15 06:28:01
【问题描述】:

我已经创建了 WCF Rest 服务,该服务从客户端获取输入/参数并将其保存在数据库和知识中,并使用添加数据的表的身份。当我通过客户端使用服务时,它只不过是 web 应用程序将 url 制作为 web请求并获得正确的输出。但是当我通过浏览器消费时出现错误。 首先我是这样尝试的:-

http://localhost:Portnumber/Test.svc/ADD/Bodywash/Orange/50

之后像这样

tp://localhost:Portnumber/Test.svc/ADD?Name=Bodywash&CategoryName=Orange&Price=50

我收到错误'Entpoint not found'。如何通过浏览器使用休息服务?我能做到吗

// Code of service

-- Table
CREATE TABLE [dbo].[Product](
    [ProductId] [int] IDENTITY(1,1) NOT NULL,
    [Name] [varchar](50) NULL,
    [CategoryName] [varchar](50) NULL,
    [Price] [int] NULL
) 

-- Sp
ALTER  procedure [dbo].[proc_InsertProduct]
(
@id int out,
@Name nvarchar(50),
@CategoryName nvarchar(50),
@Price int
)
as insert into Product
(
Name,
CategoryName,
Price
)
values
(
@Name,
@CategoryName,
@Price
)

set @id = @@identity
return @id


-- Interface

 public interface ITest
    {
        [WebInvoke(UriTemplate = "ADD/{Name}/{CategoryName}/{Price}", Method = "POST", ResponseFormat = WebMessageFormat.Json)]
        int InsertProduct(string Name, string CategoryName, string Price);
    }


-- class

 public class Test : ITest
 {
  public int InsertProduct(string Name, string CategoryName, string Price)
        {
            string constr = ConfigurationManager.ConnectionStrings["SampleConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(constr);
            SqlCommand cmd = new SqlCommand("proc_InsertProduct", con);
            cmd.CommandType = CommandType.StoredProcedure;
            con.Open();
            cmd.Parameters.AddWithValue("@Name", Name);
            cmd.Parameters.AddWithValue("@CategoryName", CategoryName);
            cmd.Parameters.AddWithValue("@Price", Price);
            cmd.Parameters.Add("@id", SqlDbType.Int);
            cmd.Parameters["@id"].Direction = ParameterDirection.Output;//Output parameter 
            cmd.ExecuteNonQuery();
            con.Close();
           int result = (int)(cmd.Parameters["@id"].Value);
            return result;//returning id 
        }
    }



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

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>

  <connectionStrings>
    <add name="SampleConnectionString" connectionString="Data Source=xxx.xxx.x.x;Initial Catalog=sample;Persist Security Info=True;User ID=sa;Password=xxxx" providerName="System.Data.SqlClient"/>
  </connectionStrings>

  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>


  <system.serviceModel>

    <services>
      <service name="RestApiTest.Test" behaviorConfiguration="serviceBehavior">
        <endpoint address="" binding="webHttpBinding" contract="RestApiTest.ITest" behaviorConfiguration="web">
        </endpoint>
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
          <behavior name="serviceBehavior">
            <serviceMetadata httpGetEnabled="true"/>
            <serviceDebug includeExceptionDetailInFaults="false"/>
          </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </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>

【问题讨论】:

  • 我能做到吗 - 是的,您可以通过浏览器访问 RESTful WCF 的 GET 端点。但要确定您的问题,您可能还需要提供 WCF 代码。您尝试使用的方法似乎是 POST 方法 - 如果该方法 (ADD) 是 POST 方法,那么您无法通过浏览器访问它。改为用户提琴手/邮递员
  • 是的,我的请求是post。我正在将记录插入到表中。我将修改我的问题。
  • 如前所述,如果方法是POST,请使用Fiddler/Postman,而不是从浏览器中点击
  • 感谢@developer 的帮助。我保持所有代码相同,只是将方法名称从 post 更改为 get。我知道 get 用于选择和 post 以进行插入/更新。这里我将数据来自 url 插入我的表并返回我的表 id。在所有浏览器中都可以正常工作。这会被认为是有效的吗?或者我将进一步面临任何问题。因为客户端可能正在通过浏览器访问它。

标签: asp.net wcf


【解决方案1】:

从 cmets 继续 - 由于 cmets 的大小限制而更新为答案

虽然我不建议这样做,但我不建议这样做 - 尊重 HTTP 动词,所有 get 请求都应该是 GET,add 应该是 POST,update 应该是 PUT(补丁的情况下是 PATCH),delete 应该是 DELETE 方法。我建议您将此方法恢复为 POST。

因为客户端可能通过浏览器访问它 - 我假设您的意思是说该服务将使用 JavaScript 代码从客户端使用。如果这是真的,那么您不必担心。如果有人向您的 REST 服务发布数据(例如,使用 $.ajax$.post 在 jQuery 的情况下),您将正确获取数据。

要使此方法成为标准的 POST 方法,请更改方法签名以接受一个 comples 对象,而不是从路由中获取 Name、CategoryName 和 Price。将此int InsertProduct(string Name, string CategoryName, string Price); 更改为int InsertProduct(Product product); 之类的东西,其中'Product' 类具有必需的属性。您可以在此处找到示例实现 - POST complex object to WCF REST

【讨论】:

    猜你喜欢
    • 2012-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多