I want to build a simple service with a single GET function which accepts JSON from a browser AJAX request and returns a file. One of the tools I have at my disposal is Visual Studio 2017 and WCF sounds like it's a decent bet.
I've been completely floundering trying to squeeze any functionality out of this framework. Online tutorials are either way too high level for my puny understanding, or the solutions are outdated. What I'd like to request are some basic pointers about WCF, how it should be used, how it shouldn't be used, spoken to someone who knows very little about the principles of networking. But that's a pretty broad scope.
To keep this to a narrow example, here's the fresh source template that Visual Studio provides for a WCF project, right out of the box.
Service1.svc:
using System;
namespace BasicWcfProject
{
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
}
}
IService1.cs:
using System.Runtime.Serialization;
using System.ServiceModel;
namespace BasicWcfProject
{
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
// TODO: Add your service operations here
}
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";
[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}
[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
}
Web.Config:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="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="false"/>
</behavior>
</serviceBehaviors>
</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>
If I run this program in the debugger, and point my browser to http://localhost:56586/Service1.svc
, I get the basic service response. Okay, cool. My intuition tells me I should then access http://localhost:56586/Service1.svc/GetData/1
to test the GetData
method, right? Supposedly not. I get a completely empty response. I place breakpoints in the method and they aren't triggering. I've tried changing the access URL to http://localhost:56586/Service1.svc/GetData
. I've tried decorating the method with a UriTemplate. Nothing works.
Obviously I'm doing something wrong, but I'm so hopelessly lost that I can't begin to tell where I need to go. Any resource I've found to attempt to point me where to go is either far too technical for me or the solution just flat out doesn't work when attempted. It's really tanking my morale.
How do I get a web browser to consume this WCF service?