gpt4 book ai didi

c# - WCF post 方法实现

转载 作者:太空宇宙 更新时间:2023-11-03 20:27:23 24 4
gpt4 key购买 nike

我有一个 WCF 服务,它连接到一个从移动应用程序调用的 sql server 数据库。我有以下方法可以帮助创建预订。

public void CreateBooking(Booking booking)
{
Booking newbooking = new Booking();
sql = new SqlConnection("Data Source=comp;Initial Catalog=BookingDB;Integrated Security=True");
sql.Open();

string command =
("INSERT INTO Bookings( BookingName, BookingStart, BookingEnd, RoomID ) " +
"VALUES ("
+ "'" + booking.BookingName + "'" + ", "
+ "'" + booking.BookingStart + "'" + ", "
+ "'" + booking.BookingEnd + "'" + ", "
+ booking.RoomID + ")");

SqlCommand cmd = new SqlCommand(command, sql);
cmd.ExecuteNonQuery();
}

public void Close()
{
sql.Close();
}

标记:

<%@ ServiceHost Language="C#" Debug="true" Service="BookingServices.BookingService" CodeBehind="BookingService.svc.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>

配置文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<!--<authentication mode="None"/>-->
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</assemblies>
</compilation>
</system.web>
<system.serviceModel>
<services>
<service name="RoomBookingServices.RoomBookingService" behaviorConfiguration="RoomBookingServiceBehaviour">
<endpoint address="http://192.168.0.4:6321/RoomBookingServices/RoomBookingService.svc" binding="webHttpBinding" bindingConfiguration="webHttpBindingWithJsonP" contract="RoomBookingServices.IRoomBookingService" behaviorConfiguration="webHttpBehavior">
<identity>
<servicePrincipalName value=""/>
</identity>
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="RoomBookingServiceBehaviour">
<!-- To avoid disclosing metadata information,
set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="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="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webHttpBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true"></binding>
</webHttpBinding>
</bindings>
<!--<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />-->
<!--<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />-->
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
<connectionStrings>
<add name="RoomBookingDatabaseEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=HAL;initial catalog=RoomBookingDatabase;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
<add name="RoomBookingDatabaseEntities1" connectionString="metadata=res://*/RoomBookingDB.csdl|res://*/RoomBookingDB.ssdl|res://*/RoomBookingDB.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=HAL;initial catalog=RoomBookingDatabase;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>

接口(interface):

[OperationContract(Name="postmethod")]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, UriTemplate = "postmethod/new")]

void CreateBooking(Booking booking);
}

预订舱位:

[DataContract]
public class Booking
{
[DataMember]
public int BookingID { get; set; }

[DataMember]
public string BookingName { get; set; }

[DataMember]
public DateTime BookingStart { get; set; }

[DataMember]
public DateTime BookingEnd { get; set; }

[DataMember]
public int RoomID { get; set; }

}

但是,每当我调用该方法时,我都会收到 405 错误。我的问题是,上面的方法是导致错误还是连接端的东西?谢谢。

最佳答案

我已经尝试了上面的场景,并进行了一些改动,如下所示:

[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, UriTemplate = "postmethod/new")]
Booking CreateBooking(Booking booking);

您可以删除 WrappedRequest 设置,因为您只有 1 个参数。

当我使用以下请求从 Fidder 执行 POST 时,我得到了成功的响应:

POST http://localhost/SampleApp/Service1.svc/postmethod/new HTTP/1.1
Content-Type: application/json
Host: localhost
Content-Length: 144
Expect: 100-continue

{"BookingEnd":"\/Date(1332420656202+0000)\/","BookingID":1,"BookingName":"client sent","BookingStart":"\/Date(1332334256202+0000)\/","RoomID":2}

您也可以删除 OperationContract 中的名称属性。如果您在 IIS 中托管,则地址可以为空,因为地址是由 IIS 分配的。

关于c# - WCF post 方法实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9802644/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com