webhttpbinding في WCF: كيفية تكوينه بالطريقة المناسبة؟
-
04-10-2019 - |
سؤال
محاولة اتباع هذا المثال لجعله يعمل: http://weblogs.asp.net/kiyoshi/archive/2008/10/08/wcf-using-webhtpbinding-for-rest-services.aspx
هنا أنا App.config
:
<system.serviceModel>
<services>
<!-- The service for the TEST WEB client -->
<service name="MyServer.AAServiceType" behaviorConfiguration="Default">
<endpoint address="testservice"
binding="webHttpBinding" behaviorConfiguration="webBehavior"
contract="MyServer.AAIContractName" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8787/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<!-- TEST WEB BEHAVIOR -->
<behavior name="Default">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
<!-- TEST WEB ENDPOINT -->
<endpointBehaviors>
<behavior name="webBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
تحديث: عقد الخدمة هو:
namespace MyServer
{
[ServiceContract(SessionMode=SessionMode.NotAllowed)]
public interface IContractName
{
[WebGet(UriTemplate = "date/{year}/{month}/{day}", ResponseFormat = WebMessageFormat.Xml)]
[OperationContract]
string GetDate(string day, string month, string year);
}
public class ServiceType : IContractName
{
public string GetDate(string day, string month, string year)
{
return new DateTime(Convert.ToInt32(year), Convert.ToInt32(month), Convert.ToInt32(day)).ToString("dddd, MMMM dd, yyyy");
}
}
}
المشكلة هي أنه عندما أحاول الاتصال بمنفذ 8787 (باستخدام putty
, ، على سبيل المثال) أ "رفض اتصال" يتم إرجاع الخطأ. كما ترون ، حاولت أيضًا وضع الأسماء الخاطئة في فئة العقد وتنفيذ الخدمة ولم أحصل على استثناءات. ماذا أفعل خطأ من فضلك؟
المحلول 2
كان هناك خطأ في الكود الذي أطلق الخدمة. الرمز الصحيح هو:
using (ServiceHost serviceHost = new ServiceHost(typeof(ServiceType)))
{
try
{
// Open the ServiceHost to start listening for messages.
serviceHost.Open();
// The service can now be accessed.
Console.WriteLine("The service is ready.");
Console.WriteLine("Press <ENTER> to terminate service.");
Console.ReadLine();
// Close the ServiceHost.
serviceHost.Close();
}
catch (CommunicationException commProblem)
{
Console.WriteLine(commProblem.Message);
Console.ReadLine();
}
}
نصائح أخرى
هل تستضيف في IIS ، أم تستضيف الذات ؟؟
إذا كنت تستضيف هذا في IIS (باستخدام ملف *.SVC) ، فإن IIS تملي العنوان - سيكون ذلك
http://yourserver/yourvirtualdirectory/yourservice.svc/.........
إذا كنت مضيفًا ذاتيًا ، فإن كل شيء يبدو جيدًا بالنسبة لي - في هذه الحالة ، يلعب عنوانك الأساسي:
http://localhost:8787/testservice
يجب أن يكون عنوان خدمتك الآن.
لا تنتمي إلى StackOverflow