RESTful Web サービスに ac# クライアント アプリケーションを作成しようとしています。
Web 参照 ( ServiceReference1
) をクライアント アプリケーションに追加しましたが、'UserService1.svc' のエンドポイント構成が存在しないなどのエラーが発生します。
Rest サービスを使用する場合、「サービス参照の追加」が必要なすべての構成を作成しないことは承知していますが、どこが間違っているのか本当にわかりません!
ここに私のファイルがあります
クライアント アプリケーション
namespace WebServiceClient
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
UserService1Client webService;
List<User> userList = new List<User>();
public MainWindow()
{
InitializeComponent();
webService = new UserService1Client();
serviceMethods();
}
private void serviceMethods()
{
string[] results = webService.GetUsersNames();
}
}
}
クライアント -web.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IUserService1" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:53215/UserService1.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IUserService1" contract="ServiceReference1.IUserService1"
name="BasicHttpBinding_IUserService1" />
</client>
</system.serviceModel>
</configuration>
サービス - Web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
</configSections>
<system.web>
<compilation debug="true" targetFramework="4.0">
</compilation>
</system.web>
<system.serviceModel>
<services>
<service name="WcfRestSample.UserService1">
<endpoint address="" contract="WcfRestSample.IUserService1" binding="webHttpBinding" behaviorConfiguration="restBehavior"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="restBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
<connectionStrings>
<add name="cs4_databaseEntities" connectionString="metadata=res://*/cs4_model.csdl|res://*/cs4_model.ssdl|res://*/cs4_model.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\cs4_database.mdf;integrated security=True;user instance=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
IUserService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace WcfRestSample
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IUserService1" in both code and config file together.
[ServiceContract]
public interface IUserService1
{
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Xml)]
List<string> GetUsersNames();
}
}
UserService1.svc
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WcfRestSample
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "UserService1" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select UserService1.svc or UserService1.svc.cs at the Solution Explorer and start debugging.
public class UserService1 : IUserService1
{
public List<string> GetUsersNames()
{
using (cs4_databaseEntities entities = new cs4_databaseEntities())
{
return entities.Users.Select(user => user.Name).ToList();
}
}
}
}
うまくいけば、誰かがここで私を助けてくれます!!