ProtocolViolationException in Octopus.Client When Adding Variable

I am trying to programmatically add variables to a variable set and getting an exception:

ProtocolViolationException
You must write ContentLength bytes to the request stream before calling [Begin]GetResponse.
at System.Net.HttpWebRequest.GetResponse()
at Octopus.Client.OctopusClient.DispatchRequest[TResponseResource](OctopusRequest request, Boolean readResponse)
at Octopus.Client.OctopusClient.Update[TResource](String path, TResource resource, Object pathParameters)
at Octopus.Client.Repositories.BasicRepository`1.Modify(TResource resource)
at UserQuery.Main()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

This is the code I am using:

var server = "http://octopus.somewhere.com";
var apiKey = "API-xxxxxxxxxxxxxxxxxxxxxxxx";

var endpoint = new OctopusServerEndpoint(server, apiKey);
var clientFactory = new OctopusClientFactory();
using (var client = clientFactory.CreateClient(endpoint))
{
	var octopus = new OctopusRepository(client);

	var libraryVariableSet = octopus.LibraryVariableSets.FindByName("Our Set Name");
	var variableSet = octopus.VariableSets.Get(libraryVariableSet.VariableSetId);

	var variable = new VariableResource()
	{
		Name = "TestName",
		Value = "TestValue",
		IsEditable = true,
		IsSensitive = false,
	};

	variableSet.Variables.Add(variable);
	var result = octopus.VariableSets.Modify(variableSet);
	result.Dump();
}

I am able to connect, retrieve and display the variables without issue.

Thanks for your help!
Tyler

Hi Tyler,

I’ve tried running your script locally and it’s working for me. Could I check which version of server and Octopus.Client you are using?

Could I also check whether you have any proxy requirements or anything like that on your network?

Regards
Shannon

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.