8,327
社区成员
发帖
与我相关
我的任务
分享
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="SecureCloudWebService.SecureCloudAspNetAjaxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="EnableMetadataBehaviors">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<!--bindings>
<webHttpBinding>
<binding name="test" crossDomainScriptAccessEnabled="true"></binding>
</webHttpBinding>
</bindings-->
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<services>
<service name="SecureCloudWebService.SecureCloud" behaviorConfiguration="EnableMetadataBehaviors">
<endpoint address="" behaviorConfiguration="SecureCloudWebService.SecureCloudAspNetAjaxBehavior" binding="webHttpBinding" contract="SecureCloudWebService.SecureCloud" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
<connectionStrings>
<!--add name="UserDeviceDbContext"
connectionString="Data Source=localhost;Initial Catalog=test;User Id=sa;Password=123456"
providerName="System.Data.SqlClient"/-->
<add name="DW_iSecureCloudEntities" connectionString="metadata=res://*/Models.DW_iSecureCloud.csdl|res://*/Models.DW_iSecureCloud.ssdl|res://*/Models.DW_iSecureCloud.msl;provider=System.Data.SqlClient;provider connection string="data source=192.168.1.128;initial catalog=DW_iSecureCloud;user id=sa;password=861004;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "json/employees")]
namespace WcfService
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“GetEmployee”。
// 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 GetEmployee.svc 或 GetEmployee.svc.cs,然后开始调试。
public class GetEmployee : IGetEmployee
{
public List<Employee> GetAllEmployeesMethod()
{
List<Employee> mylist = new List<Employee>();
using (SqlConnection conn = new SqlConnection("server=(172.168.40.99);database=Emp;userid=sa;password=zaq1@WSX"))
{
conn.Open();
string cmdStr = String.Format("Select firstname,lastname,salary from EmpInfo");
SqlCommand cmd = new SqlCommand(cmdStr, conn);
SqlDataReader rd = cmd.ExecuteReader();
if (rd.HasRows)
{
while (rd.Read())
mylist.Add(new Employee(rd.GetString(0), rd.GetString(1), rd.GetDecimal(2)));
}
conn.Close();
}
return mylist;
}
}
[DataContract]
public class Employee
{
[DataMember]
public string firstname { get; set; }
[DataMember]
public string lastname { get; set; }
[DataMember]
public decimal salary { get; set; }
public Employee(string first, string last, decimal sal)
{
firstname = first;
lastname = last;
salary = sal;
}
}
}
IGetEmployee.cs
namespace WcfService
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IGetEmployee”。
[ServiceContract]
public interface IGetEmployee
{
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "json/employees")]
//method
List<Employee> GetAllEmployeesMethod();
}
}
web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<services>
<service name="WcfService.GetEmployee" behaviorConfiguration="EmpServiceBehaviour">
<endpoint address="" binding="webHttpBinding" contract="WcfService.IGetEmployee" behaviorConfiguration="web">
</endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="EmpServiceBehaviour">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp helpEnabled="true" automaticFormatSelectionEnabled="true" />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
<system.webServer>
<directoryBrowse enabled="true" />
</system.webServer>
</configuration>