简体   繁体   中英

Service Only Works While Debugging

I have created a WCF service and am having some trouble testing it once it has been deployed. Here is the PowerShell I am using to test it:

$service = New-WebServiceProxy -Uri http://localhost:16651/Service.svc
$service.GetList()

When debugging the service from Visual Studio with F5 , I can call this script without any issue. GetList() returns a long list of telephone numbers.

However, when I host the site on IIS and run the above script, I get an empty return value.


Service Factory

So following this question , I added this attribute to Service.svc :

Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory"

However, this resulted in my script returning an error on the first line:

New-WebServiceProxy : Object reference not set to an instance of an object.

Which does not make any sense to me, as I am not referencing any empty objects... (this error appears when debugging and when hosting over IIS).


Web.Config

Next, I tried updated my web.config as per the linked question:

<services>
  <service name="LyncWebService.Service">
    <endpoint binding="webHttpBinding" contract="LyncWebService.IService" behaviorConfiguration="web"/>
  </service>
</services>

<behaviors>   
    <serviceBehaviors>
    </serviceBehaviors>
    <endpointBehaviors>
      <behavior name="web">
        <webHttp/>
      </behavior>
    </endpointBehaviors>
</behaviors>

However, now when I try to run my PowerShell script I get this error both during debugging and when hosting on IIS (again on the first line):

The HTML document does not contain Web service discovery information.


I am totally lost here and have no idea what is going wrong. I suspect it is to do with my config file, as it did seem to work when debugging from VS before I messed with the configuration.

Any help or guidance is much appreciated - and please let me know if I can provide any other information or test anything.

Here is the code that makes up my service currently:

Service.svc.cs

namespace LyncWebService
{
    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        [WebInvoke]
        List<string> GetList();
    }
    public class Service : IService
    {
        public List<string> GetList()
        {
            return Ps.GetAssignedNumbers(@"
                Set-ExecutionPolicy RemoteSigned
                Import-Module Lync
                $(Get-CSUser).LineUri"
            );
        }
    }
}

Web.Config

<?xml version="1.0"?>
<configuration>
  <appSettings/>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
    <httpRuntime/>
  </system.web>
  <system.serviceModel>

    <services>
        <service name="LyncWebService.Service">
        <endpoint binding="webHttpBinding" contract="LyncWebService.IService" behaviorConfiguration="web"/>
        </service>
    </services>

    <behaviors>   
        <serviceBehaviors>
        </serviceBehaviors>

        <endpointBehaviors>
          <behavior name="web">
            <webHttp/>
          </behavior>
        </endpointBehaviors>
    </behaviors>

    <protocolMapping>
      <!--<add binding="basicHttpsBinding" scheme="https"/>-->
      <add binding="webHttpsBinding" scheme="https"/>
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="false" 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>

Thanks to Jonathan Coffey, I realised that the service was being run by the LocalSystem account.

After changing this to my own user account and hosting the original web.config on IIS , I am now able to retrieve the full list using my PowerShell script.

  1. Open IIS
  2. Application Pools
  3. Right-Click the Application pool
  4. Advanced Settings...
  5. Process Model -> Identity
  6. Custom Account (Don't forget to include the domain for the User Name!)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM